spring-hateoas icon indicating copy to clipboard operation
spring-hateoas copied to clipboard

Pageable not generated when link to method with Pageable param

Open yzc717 opened this issue 7 years ago • 4 comments

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}"}}}

yzc717 avatar Apr 27 '18 14:04 yzc717

I think this relates to https://github.com/spring-projects/spring-data-commons/issues/2168

reda-alaoui avatar Jun 24 '20 08:06 reda-alaoui

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?

reda-alaoui avatar Jun 24 '20 08:06 reda-alaoui

Hi, any update on this?

Puspendert avatar Dec 31 '21 13:12 Puspendert

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
        }
    }
}

jefferybradberry avatar Jul 17 '22 02:07 jefferybradberry