r/SpringBoot • u/Tony_salinas04 • Dec 28 '25
Question Feedback for my Spring Boot project
https://github.com/tonysalinas-futdev/JavaEcomercceAPII'm building an e-commerce API for my portfolio. It's my first time working with Spring Boot, as I usually use Python and FastAPI. I'm also trying to make it as comprehensive as possible, using a global exception handler, DTOs, mappers, logging, custom exceptions, a modular architecture, and running tests. I welcome feedback on what I could improve.
14
Upvotes
16
u/WaferIndependent7601 Dec 28 '25
As usual: don’t put services in service package and controllers in controller packages
Use some linter and format your code correctly. Also install sonarqube to see the most issues yourself.
Don’t autoworker fields, use constructor injection. If using constructor injection, the field should be final.
Don’t use capitalized package. Packages should all be lowercase
Use actuator for health endpoint
Mapstruct can return a list for you. No need to stream each element.
Return immediately if possible. Don’t assign to a temp variable
Use spring data and don’t write simple sql statements yourself
Remove unused methods
Don’t use ifs in tests. Create a seperate test for each test case
Deleting an entity should never result in a 404.
2
u/Altruistic-Mind2791 Dec 28 '25
theres some idiomatic mistakes (packages naming should be lowercase).
the rest api also isnt right (Read rest maturity model).
i highly recommend you configure a lint, and for the package organization, if you’re studying for a interview i suggest you read Hexagonal Architecture, if its just for fun, try to make it as simple as possible.
2
u/Logical-Battle8616 Dec 28 '25
Some hint: put a README.md in your project, use yamls instead of prop files, use spotless for consistent formatting, use query methods instead of @Query.
1
u/Sahara96 Dec 28 '25
write packages name in lower case
1
u/Fine-Jacket3311 Dec 28 '25
You should add swagger documentation and integration tests. Avoid field injection, use lombok and constructor injection instead. Add version on your endpoints, it should be something like api/v1/categories, also please read restful best practices. Instead of CategoryDTO it's better to use CreateCategory, UpdateCategory, ViewCategory etc. I like to avoid using of dtos inside of service layer, find some articles about this pros and cons. For queries use query by method name mechanism, it's more readable for short and simple queries.
1
u/External_Mushroom115 Dec 30 '25
With gradle I'ld suggest you stick to the Kotlin DSL instead of Groovy. The former is statically typed and thus has better autocompletion within your IDE
1
u/MaDpYrO Dec 30 '25
Your service layer throws Database exceptions, but the service should expose such implementation details, you should throw exceptions related to business logic if you want to throw anything specific
1
u/External_Mushroom115 Jan 20 '26
Spotted as couple more issue you might want to be more consistent about.
Naming
Within
interface TokenRepositoryyour are using 2 distinct method naming strategies:findXYZToken()andgetABCToken(). Be consistent in the naming to improve readability: findXYZ, loadXYZ, retrieveXYZ, ... pick whatever you prefer but stick to 1 single strategy in all methods of that repo. Possibly in all your repos.I'ld avoid using the
getprefix for these data query methods. The "get" prefix has very specific implied semantics in Java. In that it suggests merely returning the value of a similarly named private field. Getters (=methods with zero params and a name starting with "get") are expected to be cheap methods whereas you repository methods are DB query, thus anything but cheap in that sense.Lombok
Be careful with Lombok annotations on entities. Particularly with any annotations that generate the equals() and hashCode() methods.
Take Product for example: the "id" field is mutable and also used in hashCode()/equals() yet the value of the field is lazily assigned by the database. So if you create a new Product, put that instance in a HashSet and then persist that Product to the database. HashSet.contains(product) might not behave as expected because the hashCode() value of that product suddenly changed (due to "id" suddenly having a different value). This can lead to nasty bugs.
Also, in blindly implementing (generating) hashCode() and equals() like this you have skipped an important question for the business: Under what conditions are 2 Products considered identical?
I doubt the business's answer to that question is "recursively compare the properties of both Products".
Packages
You have slightly overdone the (sub)package structure. It's fine to have 20+ classes in a package. There is no added value in separating mappers, repositories, dtos etc in subpackages.