Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-HATEOAS without extending ResourceSupport

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?

like image 204
Andres Avatar asked Dec 03 '13 10:12

Andres


People also ask

How do you implement HATEOAS in REST spring boot?

To implement HATEOAS, we would need to include related resources in the response. Instead of Student we use a return type of EntityModel<Student> . EntityModel is a simple class wrapping a domain object and allows adding links to it. We create a new resource.

How is the core principle of HATEOAS achieved?

With HATEOAS, a client interacts with a network application whose application servers provide information dynamically through hypermedia. A REST client needs little to no prior knowledge about how to interact with an application or server beyond a generic understanding of hypermedia.

Should I use spring HATEOAS?

Spring HATEOAS provides common abstractions (representational models, a Link class, API to build links pointing to Spring MVC controllers, etc.) to ease building hypermedia driven REST APIs with Spring MVC in general. Thus, you can use it alongside Spring MVC to manually build those services.

How do I use HATEOAS link?

Suppose, we have requested a GET request for localhost:8080/users/1, it returns the details of user id 1. Along with this, it also returns a field called link that contains a link (localhost:8080/users) of all users so that consumers can retrieve all the users. This concept is called HATEOAS.


1 Answers

You can use the Resource wrapper:

MyModel model = ...
Resource<MyModel> resource = new Resource(model);
resource.add(linkTo(...
like image 98
a better oliver Avatar answered Sep 19 '22 22:09

a better oliver