Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put helper classes in spring mvc [closed]

I have a helper class that generates a pdf for my API request.I am new to spring (am using spring boot) ,I want to follow strict packaging structure .

I am confused regarding :-

1.What package name should I use. eg:com.abx.myapp.helper?

2.What kind of naming convention should I follow for these helper classes?

like image 912
Abx Avatar asked Sep 07 '25 04:09

Abx


2 Answers

It depends on the project structure you're going to follow. I usually follow these types of project structures:

1. eg.com.myapp
├─── service
│    ├──── UserService.java
│    └──── PdfService.java
├─── controller
│    ├──── UserController.java
│    └──── PdfController.java
└─── util
     ├──── PdfUtils.java
     └──── UserDetailsVerifier.java

2. eg.com.myapp
├─── user
│    ├──── UserController.java
│    ├──── UserDetailsVerifier.java
│    └──── UserService.java
└─── pdf
     ├──── PdfService.java
     ├──── PdfController.java
     └──── PdfUtils.java

So either eg.com.myapp.util.PdfUtils.java or eg.com.myapp.pdf.PdfUtils.java. Personally I found that the second structure works best for me specially when working on larger projects, since using the first one can get out of hand pretty quickly - looking up controllers and services becomes a chore.

If your pdf utils class contains more general methods you can just name it PdfUtils.java or just Pdfs.java. Though it would be more clear to name it for example PdfGenerator.java and make it have methods only specific for pdf generation and nothing else (for example spring has Base64Utils.java for working with Base64 encoding and decoding, jpa has Specifications.java used only for specification combining).

like image 119
Edd Avatar answered Sep 10 '25 03:09

Edd


This is a very subjective topic. Personally I prefer to group classes by their common business domain, combining them into logical modules:

org.app.report
|
+- ReportController
+- ReportService
+- PdfWriter

org.app.customer
|
+- Customer
+- CustomerController
+- CustomerService
+- CustomerRepository

, instead of grouping by functional purpose of a class:

org.app.controller
|
+- ReportController
+- CustomerController

org.app.service
|
+- etc...

So following my preferable packaging, you might want to put this class right next to the rest of related business code. But keep in mind that this:

  • is project specific
  • is personal preference
  • must be set as project's own best practice which every team member must follow

If you want to reuse this helper in multiple places, consider to keep it in some kind of outer util or helper package.

like image 27
Andrey Atapin Avatar answered Sep 10 '25 03:09

Andrey Atapin