4 ms·
I prefer to code one level deeper and I mostly use plain Spring MVC controllers. That way I can still have spring security for the endpoints but it keeps the en
by vbsteven 7y ago
I prefer to code one level deeper and I mostly use plain Spring MVC controllers. That way I can still have spring security for the endpoints but it keeps the endpoints more decoupled from the repositories.
I typically have a repository generated by Spring Data, a small service layer with business logic on top of those and then an MVC controller that only talks to the service layer, never the repositories.
Each controller also has its own DTO class(es) for request bodies and responses and a small converter between DTO and entity. Kotlin extension methods make it easy to add the toDto() method onto the entity so a typical controller will fetch the entity from the service and return entity.toDto().
Kotlin, Spring Boot and Spring Data are amazingly well suited for this.
- victor106 7y agoSpring Framework and spring boot in particular have made enormous progress in recent times and combined with the performance of the JVM it’s one of the best ecosystems to do this in. Also, you could use projections in place of DTO’s.
- steve_taylor 7y agoI was doing things manually to, even security! You don't really need DTOs because you can use projections and set a default projection to be used when that entity type is returned in a collection. Any entity fields that should never be exposed can be annotated with @JsonIgnore. And then if you need endpoints that aren't CRUD, you can build those the usual way.
- vbsteven 7y agoI’ll check out the projections as they seem interesting and I don’t know them very well.