Alessandro C
Alessandro C

Reputation: 3560

Spring Data Rest: how to customize the "_links" property representation?

I'm trying to customize the urls linked in the "_links" property of the Spring Data Rest JSON entity representation.

Following the documentation at the point "16.5.3. Customizing the representation" (see http://docs.spring.io/spring-data/rest/docs/current/reference/html/#_customizing_the_representation), I should create a custom ConversionService with my own Converter. But how exactly can I do that?

By the way, I don't understand the reason why I have to do this, since Spring Data Rest uses a RepositoryEntityController that builds the URLS calling a "toResources" method of the class AbstractRepositoryRestController, that builds the urls calling its own "getDefaultSelfLink" method.

Why not @Override one (or both) of those methods in some way?

My doubt is: how the converter can replace the behaviour of this implementation? It's because is called after the URL construction? If the answer is yes, the problem is that the converter can modify an URL that is already been built in a previous moment, while I would prefer to create the url with my customization only one time (and not converting it after).

It's possible in any way? How to customize the RepositoryEntityController behaviour?

Thanks.

Upvotes: 2

Views: 1449

Answers (1)

adam p
adam p

Reputation: 1214

Implement a ResourceProcessor for your entity class as per here. This will allow you to add your custom links to each of the Resource<> objects for a given entity class. If you wish to remove/modify the links that Spring Data Rest generates, you will need to implement a Converter, as ResourceProcessors are executed before the SDR links are added.

Upvotes: 2

Related Questions