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?
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).
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:
If you want to reuse this helper in multiple places, consider to keep it in some kind of outer util
or helper
package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With