Error on generating self link on pageable resource

非 Y 不嫁゛ 提交于 2019-12-10 16:39:21

问题


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

}

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

In my opinion, self link should be http://localhost:8080/datas?param=12&page=2&size=10.

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

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


回答1:


In my opinion, self link should be http://localhost:8080/datas?param=12&page=2&size=10.

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));

(source)

Buggy versions of that class are doing this:

resources.add(createLink(base, null, Link.REL_SELF));

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());



回答2:


According to Oliver's comment in an issue opened to address this, the self link should not contain template information, and this is not a bug.



来源:https://stackoverflow.com/questions/44175562/error-on-generating-self-link-on-pageable-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!