Pageable not generated when link to method with Pageable param
Given
Spring boot starter 2.0.1.RELEASE with hateoas, data-jpa
@SpringBootApplication
@EnableSpringDataWebSupport
public class Application { //}
@Component
public class PersonPublicReleaseAssembler extends ResourceAssemblerSupport<PersonPublicRelease, PersonPublicReleaseResource> {
@Override
public PersonPublicReleaseResource toResource(PersonPublicRelease personPublicRelease) {
/..........
personPublicReleaseResource.add(links);
return personPublicReleaseResource;
}
}
@Autowired
PagedResourcesAssembler<PersonPublicRelease> assembler;
@GetMapping("/search/finadAllWithPagnation")
public ResponseEntity<PagedResources<PersonPublicReleaseResource>> findAllWithPagnation(@PageableDefault Pageable pageable) {
Page<PersonPublicRelease> collection = personPublicReleaseRepository.findAll(pageable);
return ResponseEntity.ok(assembler.toResource(collection, new PersonPublicReleaseAssembler()));
}
Link to findAllWithPagnation() in search mapping(below)
@GetMapping("/search")
public ResponseEntity<ResourceSupport> getSearch() {
ResourceSupport resourceSupport = new ResourceSupport();
Link pageLink = linkTo(methodOn(PersonPublicReleaseController.class)
.findAllWithPagnation(null)).withRel("findAllByPagnation");
// Assemble the resource with links
resourceSupport.add(pageLink );
return ResponseEntity.ok(resourceSupport);
}
Self link returned in the finallAll gets generated fine with first, next, last etc, however
/search would render
{"_links":{"finadAllWithPagnation":{"href":"http://localhost:8080/edw-rest/api/personpr/search/finadAllWithPagnation"}}}
Was Expecting
{"_links":{"finadAllWithPagnation":{"href":"http://localhost:8080/edw-rest/api/personpr/search/finadAllWithPagnation{?page,size,sort}"}}}
I think this relates to https://github.com/spring-projects/spring-data-commons/issues/2168
Today, UriComponentsContributor is an SPI allowing to enhance UriComponentsBuilder.
I didn't find a way to pass TemplateVariable to UriComponentsBuilder.
Therefore, I think we are missing an SPI for TemplateVariable contribution.
Should a new method getTemplateVariables be added to UriComponentsContributor interface or should it be added to a new distinct interface TemplateVariableContributor?
Hi, any update on this?
I found a workaround where I declare @RequestParam values anyway and ignore those values in the method body. Injection still gives me a Pageable I can use.
@GetMapping
public CollectionModel<?> get() {
return CollectionModel.empty().add(
linkTo(methodOn(this.getClass()).getPage(null, null, null, null, null)).withRel("pageable"));
}
@GetMapping("/page")
public ResponseEntity<PagedModel<?>> getPage(
@RequestParam(defaultValue = "0", required = false) Integer page,
@RequestParam(defaultValue = "10", required = false) Integer size,
@RequestParam(defaultValue = "UNSORTED", required = false) String sort,
Pageable pageable,
PagedResourcesAssembler<?> pagedResourceAssembler) {
return new ResponseEntity<>(service.findAll(pageable, pagedResourceAssembler), HttpStatus.OK);
}
The workaround ends up giving me what I'm looking for:
{
"_links": {
"pageable": {
"href": "http://localhost:8080/api/page{?page,size,sort}",
"templated": true
}
}
}