我有以下控制器方法:
@RequestMapping(method = GET, produces = APPLICATION_JSON_VALUE)
@Transactional(readOnly = true)
public ResponseEntity list(Pageable pageable, PagedResourcesAssembler pagedResourcesAssembler) {
Page<Customer> customers = customerRepository.findAll(pageable);
return ResponseEntity.ok().body(pagedResourcesAssembler.toResource(customers, customerResourceAssembler));
}
@RequestMapping(value = "/search", method = GET, produces = APPLICATION_JSON_VALUE)
@Transactional(readOnly = true)
public ResponseEntity search(@RequestParam("q") String q, Pageable pageable, PagedResourcesAssembler pagedResourcesAssembler) {
Specification spec = where(..some specs..);
Page<Customer> customers = customerRepository.findAll(spec, pageable);
return ResponseEntity.ok().body(pagedResourcesAssembler.toResource(customers, customerResourceAssembler));
}
第一种方法将所有客户资源作为页面返回。
第二个也返回分页结果,但可以选择提供q
查询参数进行过滤。
来自搜索方法的 JSON HATEOAS 响应包含一个下一页链接,例如:
{
"rel": "next",
"href": "http://localhost:8080/api/customers/search?page=1&size=10{&sort}"
}
问题是这里的q
查询参数丢失了。
我应该在这里使用PagedResourcesAssembler
不同的吗?