r/SpringBoot • u/AlarmOpening2062 • Mar 01 '26
Discussion E-commerce with Spring Boot
Hello everyone, I hope you're all doing well. I'm writing to ask for your support for this project I'm sharing here. Whether it's by submitting an issue, a PR, or giving a star, this is my first big project. Thank you all!
28
Upvotes
27
u/kspr2024 Mar 02 '26
Some feedback:
- Upgrade to Spring Boot 4
- No need for Dockerfile, Spring Boot can build Docker Image using Buildpacks support by simply running "mvn spring-boot-build-image"
- Prefer using Constructor injection instead of Field Injection using `@Autowired`
- Security Configuration: Instead of allowing all unspecified URLs using `requestMatchers("/**").permitAll()`, you should block all URLs by default and explicitly allow the known URLs to make it secure by default.
- Use proper HTTP Method for read, write operations: Used `@GetMapping` for "/addCart", "/cartUpdate", etc which should be POST or PUT, etc.
- I would recommend using a DB Migration tool like Flyway or Liquibase instead of using `spring.jpa.hibernate.ddl-auto=update`
For reference you can checkout my BookStore application https://github.com/sivaprasadreddy/bookstore
3
u/ComprehensiveSmell40 Mar 02 '26
Hey can I ask how did u learn springboot for this project?
3
u/Lost-Instruction-545 Mar 03 '26
It looks like you have explained project on github very efficiently. But even though readme file is good written, I had hard time moving around and understanding what does what. You have all your operations in controller class. And different logics for same entity are in same class. But you have interfaces for them to keep structure so that's valid. In my project, I have different services for different entities seperated and each entity has their own controller. And project structure more likely this -> "entities/user/services/GetUserService.class". So I don't get lost when I need to find something and neither others. Also I have only one interface for every service class which is having one method that ruled to get one any input type and one any output type. So I made a rule that will work across every service class. It's in private for now because I haven't finished html part. Btw, you might want to include test classes for methods that you wrote. It will be more professional to test them using JTest instead of using a temporary database.
3
u/Otherwise_Expert_4 Mar 04 '26
It's a great project idea! Congratulations on the progress you've made so far! Keep up the good work.However, there are some major problems.
For example:
Currently, transactions start when you call JpaRepository.save() and end when the method returns.
https://docs.spring.io/spring-framework/reference/data-access/transaction/declarative/annotations.html
This is bad because:
1.1: Every business function (use case) should be a single transaction. For example, in CartServiceImpl.cartSave():
productService.saveProduct(product);return cartRepository.save(newCart);The first line saves the product, and the transaction commits. Then, in a different transaction, you save the cart. If you add logic between these lines and one of the new lines fails (throws a RuntimeException), your data in the database would be inconsistent. In saveProduct(), you decrease the stock by one and save it, but the newCart is never saved because of the exception.
1.2 Transaction = DB connection = persistence context. Having many small transactions would cause performance problems:
- At database level, too many connections are used.
- At the JPA/Hibernate level (the persistence context cannot cache).
1.3 Your application only works because Spring uses the Open Session in View (anti-)pattern by default. You should fix the transaction boundaries, and then switch OSIV off: https://docs.spring.io/spring-boot/appendix/application-properties/index.html#application-properties.data.spring.jpa.open-in-view
2. The controller should not contain business logic. Sending an email and uploading an image are business logic and should be implemented in services.
3. Separate entity and DTO (data transfer object). In the service layer, convert the entity to a DTO. Controllers should only use DTOs, and in the service, you should convert or copy entity fields to DTOs. Provide controllers with only the minimum necessary data. (Entities may have lazy-loaded fields. Using entities for rendering views could also cause performance problems.
4. The DB model is a bit messy right now. You have a CART entity/table, but the ER in the readme shows ORDER_ITEM. It is also difficult to understand why the cart has a ManyToOne relationship with the product. I'm missing a cart_item entity/table here.
5. You don't need both DAOs and repositories. Keep only the repositories, and implement the business logic in services.
2
u/Zchwarzer Mar 03 '26
Interesting!!! I'm really appreciate what you've done, here is a little my feedback
- If it possible please upgrade to Java 21 or 25
- If it possible please use Spring Boot 4
- You can use `OpenRewrite` to migrate your project.
- At the application.properties you should change it into application.yml or application.yaml instead, because this format is easier to read.
- All the `@Autowired` should be change it into Constructor injection.
- I don't know how to explain about util package, but what it should be for this package is for utilize function or method and that shouldn't inject any class into them and what I've see a lot of util class is they only have static function or method, Seriously I don't know why but I follow that practice for a long time (If I missing something please correct me). I suggest to learning util class from `org.apache.commons.lang3` they've a lot of util class example StringUtils, RegExUtils, ArrayUtils
- For `AppConstant` and `StatusOrder` those 2 aren't util you may create `constant` package and move those 2 into that
- For method that declare in interface is unnecessary to define access modifier (access modifier is public protected private and default)
If you don't mind may I PR to your project and we can discuss some more If you want, By the way I have to say again that what you have done is a good things keep it better.
Best regards