Display collection object reference using spring hateoas

I have the same question that was asked here ( Displaying a reference to a collection object in spring REST data ). But none of this topic helps me add a custom link to the collection call.

@Component public class EventListResourceProcessor implements ResourceProcessor<Resources<Event>> { @Autowired private RepositoryEntityLinks entityLinks; @Override public Resources<Event> process(Resources<Event> events) { events.add(entityLinks.linkToCollectionResource(Event.class).withRel("events")); return events; } } 

a process method is never called in this case.

I need to call http: // localhost: 8080 / event and get the following JSON with my_custom_link in the _links section:

 { "_embedded": { "event": [ { "id": "1", "name": "Updated event" }] }, "_links": { "self": { "href": "http://localhost:8080/event" }, "profile": { "href": "http://localhost:8080/profile/event" }, "my_custom_link": { "href": "http://localhost:8080/custom/" } }, "page": { "size": 20, "totalElements": 4, "totalPages": 1, "number": 0 } } } 

Could you advise me?

Thanks in advance!

+1
rest spring-data-rest spring-hateoas
source share
1 answer

I was in a similar situation with your question: I read the question / answers you linked and found that none of them can solve the problem. Here is a possible answer to my problem:

 @Component public class MyResourceProcessor implements ResourceProcessor<Resource<MyInterface>> { @Autowired private EntityLinks entityLinks; @Override public Resource<MyInterface> process(Resource<MyInterface> event) { event.add(entityLinks.linkForSingleResource(MyClass.class, event.getContent().getId()).slash("custom").withRel("custom")); return event; } } 

This process method was called for each resource in the corresponding set of returned resources. The interface of MyInterface and MyClass should be replaced with what you need, but you had to write it so that it worked. Here are the steps that I used to properly call the process method and determine the type MyInterface .

  • I created a process method that simply took ResourceSupport as a parameter. I created a breakpoint in the code and verified that the main class extends ResourceSupport . In my case, it was a PersistentEntityResource . This explains why using Resource<MyClass> or Resources<MyClass> never called the process method: PersistentEntityResource extends Resource<Object> .

  • I updated the process method, taking PersistentEntityResource as a parameter. This caused the process method to be called for more than my intended changes. I again used a breakpoint to inspect the PersistentEntityResource object in order to discover which class can be found in the Resource<Object> that it extended. I found this to be a Proxy class and could not be distinguished from MyClass , as I wanted.

  • I followed the answer found here to find out more information about the Proxy class: https://stackoverflow.com/a/232650/ ... During debugging, I discovered a list of interfaces that helped define this class. One of them was of type MyProjectionInterface . Now I knew that the reason I could not use Resource<Portal> was because it was actually Resource<MyProjectionInterface> .

  • I had three different Projections that I needed to handle. Instead of creating three separate ResourcePorcoessors I created MyInterface , and all three of my projection interfaces extended it. MyInterface only contained the Long getId() method, which Projections still supported.

  • I updated my code to use Resource<MyInterface> , and added linkForSingleResource using MyClass (which all Projections apply to) and the getId() method that I defined in MyInterface . This successfully added the desired link to each returned resource.

We hope that these steps will help others learn how to determine which type to use as a parameter to the process method.

+1
source share

All Articles