0

The Spring Data Rest repository notes that Custom Links can be added to an Entity as below: https://docs.spring.io/spring-data/rest/docs/current/reference/html/#customizing-sdr.customizing-json-output.adding-links

Example Given:

@Bean
public RepresentationModelProcessor<EntityModel<Person>> personProcessor() {

   return new RepresentationModelProcessor<EntityModel<Person>>() {

     @Override
     public EntityModel<Person> process(EntityModel<Person> model) {

      model.add(new Link("http://localhost:8080/people", "added-link"));
      return model;
     }
   };
}

I try following documents but it's not working. The final output result does not change.

How can I add link to a resource? Or maybe I miss something? Please help me!

1 Answers1

0

It's possible that the RepresentationModelProcessor bean you've created isn't being picked up by Spring. To ensure that it is registered with the application context, make sure that it is either defined as a Spring bean in a configuration class or that it is annotated with @Component so that Spring can automatically detect and register it.

Another possibility is that your EntityModel is not of the correct type, which can cause your custom link to not appear in the output. Make sure that the EntityModel you are trying to add the link to is of the correct type.

If neither of these suggestions solve the issue, there may be a problem with the version of Spring Data Rest that you are using. Try upgrading to the latest version and see if that resolves the issue.

Also, please double check the generated JSON output. The added link may not be immediately visible in the main object itself but may appear as a link in the "_links" object of the response.

Mahesh Budeti
  • 374
  • 1
  • 16