Stop Returning Entities From Your Controllers
What goes wrong when a JPA entity is also your API response, and how DTOs fix it without much ceremony.
- Spring Boot
- JPA
- REST
Writing
Short, focused explainers on the things that come up most often in the comments.
What goes wrong when a JPA entity is also your API response, and how DTOs fix it without much ceremony.
Where @Valid actually takes effect, why your constraints are being ignored, and how to write a custom one.
How to turn thrown exceptions into consistent HTTP responses using @RestControllerAdvice and ProblemDetail.
Where Spring actually reads config from, how profiles layer on top, and why secrets never belong in application.properties.
@Data on a JPA entity, @Builder with required fields, and the other Lombok shortcuts that cost more than the boilerplate they remove.
Offset pagination, its two failure modes at scale, and how to move to keyset pagination in Spring Data without breaking clients.
How one innocent-looking getter turns a single query into hundreds, and the four ways to stop it.
Buckets, treeification, resizing, and the practical consequences: sizing, mutable keys, and why HashMap is not thread-safe in an interesting way.
Which actuator endpoints to expose, how liveness and readiness differ, and the custom health indicator that takes your service down.
The proxy rule behind Spring's most confusing silent failure, and the three ways to fix it.
How Spring configures Jackson, why dates come out as numbers, and the annotations that keep an API contract stable as the code changes.
How to run work off the request thread in Spring, why the default executor is a liability, and what gets lost when you leave the thread.