![](/img/trans.png)
[英]Spring Boot, java.lang.IllegalStateException when calling ControllerLinkBuilder.linkTo from a websocket
[英]Spring Boot (HATEOAS) ControllerLinkBuilder.linkTo(…) query parameter missing/not created
我正在使用带有Spring Boot(1.4)的REST API,还使用PagedResourcesAssembler来创建带有分页的JSON响应,这就像一个魅力。 Spring的家伙做得很好! 但是,在提供URL查询参数(如果提供)时遇到了问题。 我知道PagedResourcesAssembler不能自行确定URL查询参数,因此我想使用ControllerLinkBuilder.linkTo(controller,parameters)方法为其提供一个Link:
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public HttpEntity<PagedResources<Document>> find(final DocumentSearch searchForm, final Pageable pageable, final PagedResourcesAssembler assembler) {
final Page<Document> documents = documentService.find(searchForm, pageable);
// this map is just for this example ..
final Map<String, Object> parameters = new HashMap<>();
parameters.put("text", "foobar");
final Link link = ControllerLinkBuilder.linkTo(DocumentController.class, parameters).withSelfRel();
return ResponseEntity.ok(assembler.toResource(documents, link));
}
如您所见,我为链接提供了一个参数,但是相应的响应中没有URL查询:
{
"_links": {
"first": {
"href": "http://localhost:8080/documents?page=0&size=20"
},
"self": {
"href": "http://localhost:8080/documents"
},
"next": {
"href": "http://localhost:8080/documents?page=1&size=20"
},
"last": {
"href": "http://localhost:8080/documents?page=2&size=20"
}
}
我还尝试调试它,他在打电话:
linkTo:123,ControllerLinkBuilder(org.springframework.hateoas.mvc)
展开:152,UriComponents(org.springframework.web.util)
expandInternal:47,HierarchicalUriComponents(org.springframework.web.util)
expandInternal:330,HierarchicalUriComponents(org.springframework.web.util)
expandInternal:340,HierarchicalUriComponents(org.springframework.web.util) 直到这一点,他仍然拥有我的text = foobar,同时还是QueryUriTemplateVariable,但是在
expandInternal:341,HierarchicalUriComponents(org.springframework.web.util) 他没有进入for循环,他会/可能将我的text = foobar放入他的结果图中
很感谢任何形式的帮助。
最好的问候,彼得
UPDATE
为了使这项工作,我必须做两件事。 首先,更改方法签名,并在分页链接中添加@RequestParam和我想要的相应参数作为参数,其次,使用ControllerLinkBuilder中的methodOn :
public HttpEntity<PagedResources<Document>> find(@RequestParam(value = "text", required = false) final String text, final Pageable pageable, final PagedResourcesAssembler assembler) {
final Link link = ControllerLinkBuilder.linkTo(
ControllerLinkBuilder.methodOn(
DocumentController.class).find(text, pageable, assembler)).withSelfRel();
非常感谢你们的帮助,欢呼。
PS有没有方法可以使这项工作没有methodOn吗?
链接是根据@RequestMapping
生成的。 您传递的参数仅用于替换指定URL中的变量。 它们不是添加的查询参数。 您需要自己做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.