Spring-HATEOAS without extending ResourceSupport
Asked Answered
P

2

6

I'm building a REST API. I have a domain model composed of beans than can't extend ResourceSupport. Which is the best way to expose them as resources using Spring-HATEOAS?

In case that's not possible, which is the best way to include links on the JSON generated by the beans?

Partlow answered 3/12, 2013 at 10:15 Comment(0)
D
13

You can use the Resource wrapper:

MyModel model = ...
Resource<MyModel> resource = new Resource(model);
resource.add(linkTo(...
Dario answered 12/12, 2013 at 15:42 Comment(2)
Thank you! Do you know any tutorial where I can learn more about this?Partlow
@Partlow There are some on the Spring HATEOAS homepage. The Spring Developer Channel on YouTube also has some excellent videos on building restful services using Spring HATEOAS.Dario
I
4

You should separate Resources from your Domain.

Even if they might appear to be similar, Domain model and Resources are profoundly different.

Domain objects are your internal representation. The implementation have constraints depending on how your business logic/persistence is implemented and other design decision. For example they may be JPA entities or may be immutable.

Resources are the representation to the external world. Might be one-to-one with the Domain or totally different. It is not so infrequent having multiple Resource representation for a single Domain entity.

But first of all, the Resource implementation is meant to be sent/received on the wire. So it has constraints for being marashalled/unmarshalled.

So your application should have separate objects for the domain and the resources. With Spring HATEOAS the mapping is done using Resource assemblers.

You may have a look at this sample application: https://github.com/opencredo/spring-hateoas-sample and the related post: Implementing HAL hypermedia REST API using Spring HATEOAS

Irmgardirmina answered 21/5, 2016 at 9:31 Comment(1)
While this doesn't answer the question, This is useful to know!Bronze

© 2022 - 2024 — McMap. All rights reserved.