Xeperis
Xeperis

Reputation: 1459

Spring boot, hateoas and @RestController

I have been fiddling with Spring Boot, building a headless REST-full application, trying out spring hateoas...

But here is the thing - while I do have a domain model based on java persistence API my general understanding is that while building rest controller you should not feed entities directly as a Http resource response. Say if I have entity Task.class it has all sorts of information that might not be relevant or secret to the consumer requesting this resource.

I have seen some Jackson annotations for making certain stuff be ignored while serializing responses (assuming one uses jackson) but what if I want my domain to be decoupled as much as possible ?

Now I know I can use POJO's to achieve this but maybe there is already some out of the box solution involving or at least compatible with spring .

Thank you in advance,

Upvotes: 1

Views: 411

Answers (1)

Chris DaMour
Chris DaMour

Reputation: 4010

Avoid the ResourceSupport wrappers of Spring-HATEOAS, they are really only there for the simplest PoC's in my opinion and tie you to directly to your underlying model.

Use the ResourceAssembler stuff to turn your underlying model/entities/domain layer into specific Resources. Most of my conroller methods look something like this.

GET /thing/1
var thing = thingService.find(1)
if thing == null return 404
var resource = thingAssembler.toResource(thing)
return Ok(resource)

So i have classes that extend Spring-HATEOAS' ResourceSupport class. Those define my contract with my clients, how i populate them is generally assemblers, but it can change.

Upvotes: 1

Related Questions