Mastering Backend Development with Java Spring Boot: Best Practices and Pro Tips 2

Nihal Parmar
3 min readApr 21, 2023

--

  1. Multiline Comments: It is important to write descriptive comments for each function and controller in Spring Boot. This helps other developers understand the purpose of the code and how it works.

2. Use Chat GPT: You can use Chat Gpt to help with documentation, code comments, and other tasks related to backend development in Spring Boot.

3. Use ResponseEntity: Return everything from the controller in a ResponseEntity, which includes a Dto with the response status. This provides consistency in the API response and makes it easier to handle errors.

4. Code Formatting: It is a good practice to write code within a single grey line in IntelliJ, as this improves the readability of the code in GitHub or Bitbucket.

5. Constants: Store API names in constants and use a specific format, such as “api/v1/get-item-by-name”. Keep separate constants files for API constants, Exception constants, and Response message constants.

6. User-Defined Exceptions: Create user-defined exceptions and make a custom exception handler to handle every exception, including internal server exceptions. This helps to provide better error handling for the API.

7. Error Response DTO: Use an Error Response DTO to return error messages, as well as DTOs for the request and response.

8. AOP Concepts: Use AOP concepts when necessary, such as user-generated logs when logging in.

9. Service Abstraction: In the service layer, override all methods from the interface to achieve abstraction. All other methods called by internal service methods should be private.

10. Autowiring: When autowiring a repository in the service layer, always use the required-args constructor and make the member final. Remove the autowire annotation.

11. Code Reusability: Check all services carefully to ensure that code is not repeated unnecessarily.

12. Logging: Whenever there is a need to print anything, use a logger instead of System.out.

13. String Constants: When displaying a string with constants, use string formatter instead of the “+” annotation to save space in heap memory.

14. Getters, Setters, and Constructors: Remove getter, setter, and constructor methods from classes and use Lombok annotations instead.

15. Application Properties: Describe all paths and credentials in the application.properties file and use them throughout the application.

16. Validation: Add validation to all DTOs as much as possible.

17. DTO Conversion: Use ModelMapper for simple DTO conversions, but for more complex conversions with handlers and validations, use Mapper Struct.

18. Custom Queries: For making custom queries for the repository, use JPA query or the Criteria Builder.

19. Pull Requests: After successfully completing the code, take a pull from the develop branch and resolve any conflicts before raising a pull request.

20. Code Review: Check the pull request to ensure that unnecessary files are not uploaded, and write a great description in the pull request and Jira ticket.

21. SonarCube Report: After raising the pull request, scan your branch in Bamboo and check the SonarCube report for any bugs or code smells. Use the line “ANALYSIS SUCCESSFUL, you can browse http://10.0 …” to find the SonarCube report in Bamboo.

--

--

Nihal Parmar
Nihal Parmar

Written by Nihal Parmar

Software Engineer @Crest Data System|| 6⭐ @HackerRank || Tech-Enthusiast || GSSoC’21 || Full Stack Developer

No responses yet