在可分页资源上生成自链接时出错 [英] Error on generating self link on pageable resource
问题描述
制作一个简单的 RestController
Make a simple RestController
@RestController
public class Controloler
@Value
class MyData {
int value;
}
@GetMapping(value = "/datas", produces = MediaTypes.HAL_JSON_VALUE)
public PagedResources<Resource<MyData>> getMyData(PagedResourcesAssembler<MyData> assembler,
@RequestParam(required = false) String param,
@PageableDefault Pageable pageRequest)
{
MyData data = new MyData(1);
Page<MyData> page = new PageImpl<>(Collections.singletonList(data), pageRequest, 100);
Link selfLink = linkTo(methodOn(Controloler.class).getMyData(assembler, param, pageRequest)).withSelfRel();
return assembler.toResource(page, selfLink);
}
}
当我尝试获取页面时 curl "http://localhost:8080/datas?param=12&page=2"
自链接生成有问题
When I try to get page curl "http://localhost:8080/datas?param=12&page=2"
have a problem with self link generation
{
"_embedded": {
"myDataList": [
{
"value": 1
}
]
},
"_links": {
"first": {
"href": "http://localhost:8080/datas?param=12&page=0&size=10"
},
"prev": {
"href": "http://localhost:8080/datas?param=12&page=1&size=10"
},
"self": {
"href": "http://localhost:8080/datas?param=12"
},
"next": {
"href": "http://localhost:8080/datas?param=12&page=3&size=10"
},
"last": {
"href": "http://localhost:8080/datas?param=12&page=9&size=10"
}
},
"page": {
"size": 10,
"totalElements": 100,
"totalPages": 10,
"number": 2
}
}
在我看来,自链接应该是 http://localhost:8080/datas?param=12&page=2&size=10
.
In my opinion, self link should be http://localhost:8080/datas?param=12&page=2&size=10
.
刚才我可以在不使用 pageable 参数的情况下解决这个问题,只需精确的参数页面和大小.但是,我希望有一些可分页的解决方案
Just now I can solve this problem without using pageable in arguments, just exact params page and size. But, I hope there is some solution with pageable
我已经看到在 spring-data-rest self 的情况下有一种模板.但我想得到我请求的网址
I've seen that in case of spring-data-rest self have a type of template. But I'd like to get the url I've requested
推荐答案
在我看来,自链接应该是 http://localhost:8080/datas?param=12&page=2&size=10.
我同意.事实上,这似乎是一个错误.PagedResourcesAssembler
的最新版本做了不同的处理:
I agree. In fact, it seems to be a bug. The most recent version of PagedResourcesAssembler
does it differently:
Link selfLink = link.map(it -> it.withSelfRel())//
.orElseGet(() -> createLink(base, page.getPageable(), Link.REL_SELF));
(来源)
该类的 Buggy 版本正在这样做:
Buggy versions of that class are doing this:
resources.add(createLink(base, null, Link.REL_SELF));
createLink
方法永远不会传递所需的 Pageable
,而是将 null
作为第二个参数传递.
The createLink
method is never passed the needed Pageable
, but null
as the second argument.
因此,如果您无法升级到最新版本,您仍然可以解决这个问题:
So, if you can't upgrade to the most recent version you can still work-around it:
Link selfLink = linkTo(methodOn(Controloler.class).getMyData(assembler, param, pageRequest)).withSelfRel();
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(selfLink.expand().getHref());
new HateoasPageableHandlerMethodArgumentResolver().enhance(builder, null, pageRequest);
Link newSelfLink = new Link(builder.build().toString());
这篇关于在可分页资源上生成自链接时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!