Skip to content

Pageable not generated when link to method with Pageable param #706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
yzc717 opened this issue Apr 27, 2018 · 4 comments
Open

Pageable not generated when link to method with Pageable param #706

yzc717 opened this issue Apr 27, 2018 · 4 comments

Comments

@yzc717
Copy link

yzc717 commented Apr 27, 2018

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

@reda-alaoui
Copy link
Contributor

reda-alaoui commented Jun 24, 2020

I think this relates to spring-projects/spring-data-commons#2168

@reda-alaoui
Copy link
Contributor

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?

@Puspendert
Copy link

Hi, any update on this?

@jefferybradberry
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants