saravanan-paramasivam/java-groq-rule icon
public
Published on 7/11/2025
java_groq_rule

Rules

Spring Boot Java Coding Rules and Best Practices

A comprehensive checklist and guide for writing clean, maintainable, and scalable Spring Boot applications.


🚀 1. Project Structure and Organization

  • Use the standard package structure: com.yourcompany.projectname
  • Organize by layer: controller, service, repository, model, config, dto, exception, util
  • For large apps, consider package-by-feature (e.g., user, order, payment)
  • Class Naming: PascalCase
  • Variable/Method Naming: camelCase
  • Avoid abbreviations and acronyms

🔧 2. Spring Boot Configuration

  • Use @ConfigurationProperties over @Value for structured config
  • Externalize all configuration in application.yml or application.properties
  • Use Spring Profiles (dev, test, prod)
  • Validate config with @Validated
  • Fail fast on startup misconfigurations

🧠 3. Dependency Injection and Bean Management

  • Prefer constructor injection
  • Avoid field injection (@Autowired on fields)
  • Be explicit with component scanning (define basePackages)
  • Use @Qualifier when multiple beans of the same type exist

🧾 4. DTOs and Entities

  • Never expose entity objects in API responses
  • Use DTOs for both request and response payloads
  • Use ModelMapper or MapStruct to handle mapping
  • Validate incoming DTOs using javax.validation annotations and @Valid

🧰 5. Service and Repository Layer

  • Keep services stateless
  • Push reusable logic to utility/helper classes
  • Use interface + implementation pattern for services and repositories
  • Define transaction boundaries with @Transactional at the service layer

📡 6. Controller Layer

  • Use @RestController for REST APIs
  • Handle exceptions globally with @ControllerAdvice
  • Always return DTOs or wrapper responses
  • Use ResponseEntity for custom status codes and headers

🧪 7. Testing

  • Unit test services and utilities with JUnit + Mockito
  • Use @SpringBootTest for integration tests
  • Test repositories using @DataJpaTest
  • Use MockMvc or WebTestClient to test REST endpoints
  • Isolate DB with Testcontainers if possible

🛡️ 8. Security and Validation

  • Enable and configure Spring Security
  • Use input validation to prevent injection and data corruption
  • Hash passwords using BCryptPasswordEncoder
  • Protect endpoints with proper authentication and authorization
  • Enable CSRF and configure CORS as required

🧹 9. Code Quality and Maintainability

  • Follow Java code conventions: indentation, braces, spacing, naming
  • Use a linter and formatter (e.g., Checkstyle, Spotless)
  • Use Lombok with care:
    • Avoid @Data on JPA entities
    • Prefer @Getter, @Setter, @Builder as needed
  • Log using SLF4J: LoggerFactory.getLogger(...)
  • Avoid using System.out.println
  • Create and use custom exceptions

⚡ 10. Performance and Optimization

  • Avoid N+1 query problems using join fetch, @EntityGraph
  • Use caching with @Cacheable, Redis, or Caffeine
  • Perform long-running tasks with @Async or Spring Batch
  • Configure connection pooling (HikariCP is default)
  • Profile and monitor the application with Actuator

🧭 Checklist Summary

  • [ ] Use constructor injection
  • [ ] Separate DTOs from entities
  • [ ] Validate all incoming request data
  • [ ] Write unit & integration tests
  • [ ] Configure Spring Security properly
  • [ ] Externalize all environment-specific config
  • [ ] Avoid field injection and circular dependencies
  • [ ] Use proper exception handling
  • [ ] Monitor and profile using Actuator
  • [ ] Document REST APIs with Swagger/OpenAPI