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
| File | Covers | Rules | Scope |
|---|---|---|---|
00-core.mdc | Core conventions for a Java 21 / Spring Boot 3 service | 11 | Always on |
10-api-controllers.mdc | REST controller and DTO rules | 7 | Auto-attaches to src/main/java/**/*Controller.java src/main/java/**/dto/**/*.java |
20-errors.mdc in free sample | Spring Boot 3+ error handling with ProblemDetail (RFC 9457) | 8 | Auto-attaches to src/main/java/**/*.java |
30-persistence.mdc in free sample | JPA entities, repositories, transactions and Flyway migrations | 10 | Auto-attaches to src/main/java/**/*Repository.java src/main/java/**/*Entity.java src/main/resources/db/migration/** src/main/java/**/*Service.java |
40-security.mdc | Spring Security 6 configuration and secure coding rules | 9 | Auto-attaches to src/main/java/**/security/**/*.java src/main/java/**/*Config*.java src/main/resources/application*.yml src/main/resources/application*.properties |
50-testing.mdc | Testing strategy with JUnit 5, AssertJ, slices and Testcontainers | 9 | Auto-attaches to src/test/**/*.java |
60-naming-style.mdc | Naming and code style rules for Java sources | 8 | Auto-attaches to src/**/*.java |
CLAUDE.md | Claude Code project instructions — sections: Commands, Architecture, Errors, Database, Security, Testing, Style | 22 | Always on (Claude Code) |
README.md | Install 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))`.