0
votes

In the Spring Data JPA documentation it explains how to generate links using PagedResourcesAssembler. The documentation makes reference to a toResources() method but as far as I can see that method does not exist. I have successfully generated links for the collection of resources but I can't figure out how to generate links for the sub resources.

Here is my controller:

public HttpEntity<PagedResources<Party>> search(@RequestBody PartySearchRequest request,
                                                    Pageable pageable, PagedResourcesAssembler<Party> assembler ) {

        Map<String,String> searchFilters = RequestValidator.validateContractSearchFilters(request);

        Page<Party> parties = repository.findByFirstNameOrLastName(searchFilters.get("firstName"), searchFilters.get("lastName"), pageable);

        return new ResponseEntity(assembler.toResource(parties), HttpStatus.OK);

    }

This produces the following JSON:

{
  "_embedded": {
    "partyList": [
      {
        "firstNm": "John",
        "lastNm": "Doe",
      },
      {
        "firstNm": "Bob",
        "lastNm": "Smith",
       }
        ],
      }
    ]
  },
  "_links": {
    "first": {
      "href": "http://localhost:8080/v1/party/search?page=0&size=2"
    },
    "self": {
      "href": "http://localhost:8080/v1/party/search?page=0&size=2"
    },
    "next": {
      "href": "http://localhost:8080/v1/party/search?page=1&size=2"
    },
    "last": {
      "href": "http://localhost:8080/v1/party/search?page=7&size=2"
    }
  },
  "page": {
    "size": 2,
    "totalElements": 16,
    "totalPages": 8,
    "number": 0
  }
}

As you can see I get links for the entire parties search but not for the individual party objects. (I think my question is similar to this question: How to add HATEOAS links in a sub resource) but I wasn't quite sure so I posted my own.

Any help would be greatly appreciated! Thank you!

1

1 Answers

1
votes

You need a reference to your class that extends ResourceAssemblerSupport.

This should work, changing myResourceAssembler to whatever your class is:

In your controller:

private final MyResourceAssembler myResourceAssembler;

public MyController(MyResourceAssembler myResourceAssembler) {
   this.myResourceAssembler = myResourceAssembler;
}

public HttpEntity<PagedResources<Party>> search(@RequestBody PartySearchRequest request,
                                                Pageable pageable, PagedResourcesAssembler<Party> assembler ) {

    Map<String,String> searchFilters = RequestValidator.validateContractSearchFilters(request);

    Page<Party> parties = repository.findByFirstNameOrLastName(searchFilters.get("firstName"), searchFilters.get("lastName"), pageable);

    Link selfLink = linkTo(methodOn(this.getClass().view(pageable, null)).withSelfRel();

    return new ResponseEntity(assembler.toResource(parties, myResourceAssembler, selfLink), HttpStatus.OK);

}

And, if you don't want the selfLink:

return new ResponseEntity(assembler.toResource(parties, myResourceAssembler), HttpStatus.OK);

References