← All packs

Spring Boot 3 + Java 21

Package-by-feature layout, ProblemDetail errors, Flyway + JPA rules that prevent N+1, Spring Security 6 lambda DSL, Testcontainers tests.

Download free sample (2 files)Buy · $12

Included files

FileCoversRulesScope
00-core.mdcCore conventions for a Java 21 / Spring Boot 3 service11Always on
10-api-controllers.mdcREST controller and DTO rules7Auto-attaches to src/main/java/**/*Controller.java src/main/java/**/dto/**/*.java
20-errors.mdc in free sampleSpring Boot 3+ error handling with ProblemDetail (RFC 9457)8Auto-attaches to src/main/java/**/*.java
30-persistence.mdc in free sampleJPA entities, repositories, transactions and Flyway migrations10Auto-attaches to src/main/java/**/*Repository.java src/main/java/**/*Entity.java src/main/resources/db/migration/** src/main/java/**/*Service.java
40-security.mdcSpring Security 6 configuration and secure coding rules9Auto-attaches to src/main/java/**/security/**/*.java src/main/java/**/*Config*.java src/main/resources/application*.yml src/main/resources/application*.properties
50-testing.mdcTesting strategy with JUnit 5, AssertJ, slices and Testcontainers9Auto-attaches to src/test/**/*.java
60-naming-style.mdcNaming and code style rules for Java sources8Auto-attaches to src/**/*.java
CLAUDE.mdClaude Code project instructions — sections: Commands, Architecture, Errors, Database, Security, Testing, Style22Always on (Claude Code)
README.mdInstall steps for Cursor and Claude Code, customization notes––

Preview: 20-errors.mdc (from the free sample)

# Error handling

- Use this rule with Spring Boot 3+ and Spring Framework 6+ (`ProblemDetail` does not exist in Spring Boot 2).
- Set `spring.mvc.problemdetails.enabled=true`. Spring MVC's built in exceptions then render as `ProblemDetail` (RFC 9457, `application/problem+json`). Errors that never reach a controller advice (servlet filters, the container) still go to Boot's `/error` mapping and its default JSON, so do not assume every error response is a `ProblemDetail`.
- One `@RestControllerAdvice` named `GlobalExceptionHandler` in `common.error`, extending `ResponseEntityExceptionHandler`.
- Domain exceptions extend a single `abstract class DomainException extends RuntimeException` and carry an `HttpStatus` and stable `errorCode` (e.g. `ORDER_NOT_FOUND`). `ResponseEntityExceptionHandler` does not map them on its own: add `@ExceptionHandler(DomainException.class)` that returns `ProblemDetail.forStatusAndDetail(ex.getStatus(), ex.getMessage())` with `setProperty("errorCode", ex.getErrorCode())`. (Alternative: extend `ErrorResponseException` instead of `RuntimeException`.)
- Use `NotFoundException`, `ConflictException`, `ValidationException` subclasses; throw from services, never from controllers.
- Add a fallback `@ExceptionHandler(Exception.class)` in `GlobalExceptionHandler`: return a 500 `ProblemDetail` with a generic detail and a `correlationId` property, and log the stack trace once, server-side, with that correlation id. `ResponseEntityExceptionHandler` rethrows exceptions it does not know. Never return stack traces or SQL messages to clients.
- Outside that fallback, never catch `Exception` just to log and rethrow. Catch only what you can handle.
- `Optional` is a return type only: `repo.findById(id).orElseThrow(() -> new NotFoundException("Order", id))`.