2

This is the JSON-response of a GET-Reqeust:

{
    ...
    "_links": {
        "self": {
             "href": "http://localhost:8080/persons/1"
         },
         "person": {
             "href": "http://localhost:8080/persons/1{?projection}",
             "templated": true
         },
         "anotherResource": {
             "href": "http://localhost:8080/persons/1/anotherResource"
         }
     }
}

The thing is, that I need to have the self-link of "anotherResource". Instead of:

 "href": "http://localhost:8080/persons/1/anotherResource"

I need to have link something like:

"href": "http://localhost:8080/anotherResources/2"

I know that I can implement it by doing additional request. But this solution is not practical/possible in my situation, I need to have a lot of data and it's not good to perform an additional request for each item.

Any suggestions/solutions?

watchme
  • 720
  • 1
  • 9
  • 25
TestName
  • 378
  • 2
  • 13

2 Answers2

3

You can try to use ResourceProcessor with RepositoryEntityLinks to build the link you need:

@Component
public class PersonResourceProcessor implements ResourceProcessor<Resource<Person>> {

    private RepositoryEntityLinks entityLinks;

    public PersonResourceProcessor(RepositoryEntityLinks entityLinks) {
        this.entityLinks = entityLinks;
    }

    @Override
    public Resource<Person> process(Resource<Person> resource) {
        Person person = resource.getContent();
        AnotherResource anotherResource = person.getAnotherResource()
        Link link = entityLinks.linkForSingleResource(anotherResource).withRel("anotherResource");
        resource.add(link);
        return resource;
    }
}

But be careful here, because if the resource person does not eagerly have nested anotherResource you can catch the LazyInitializationException (not sure, but check it, please...) or get additional query to DB for every person.getAnotherResource() call (the N+1 queries issue). That's why it's better to use a relative link like '/persons/1/anotherResource'.

Cepr0
  • 28,144
  • 8
  • 75
  • 101
0

Endpoints comments

Are you not using the @RequestMapping anotation to declare REST endpoints?

I would try to define the different endpoints using a different @RequestMapping calls.

In this way, the program can have one RequestMapping for /persons where any persons-related actions are associated to its respective endpoint, and define another "root" (mandatory quotes, but you sure get where i am heading to) mapping for /anotherresources where you could add the necessary endpoints.

Response processing

If all you need is to change the JSON that the user receives when accessing a GET endpoint, You could just preprocess the parameter and update the JSON before actually sending it back to the user.

But of course, if you rewrite the JSOn without backing the new value for the parameter in the JSON with an existing endpoint, the user will be having trouble when trying to access that URI. If you only need to form the JSON, I would assume that the endpoints for /anotherresources already exist.

For detailed info on @RequestMapping, be sure to visit the Spring Docs website.

I hope i helped you!

versvs
  • 643
  • 2
  • 12
  • 30