使用ControllerLinkBuilder的模板变量 [英] Template variables with ControllerLinkBuilder
问题描述
我希望我的答复中包括以下内容:
I want my response to include this:
"keyMaps":{
"href":"http://localhost/api/keyMaps{/keyMapId}",
"templated":true
}
这很容易实现:
add(new Link("http://localhost/api/keyMaps{/keyMapId}", "keyMaps"));
但是,当然,我宁愿使用ControllerLinkBuilder,如下所示:
But, of course, I'd rather use the ControllerLinkBuilder, like this:
add(linkTo(methodOn(KeyMapController.class).getKeyMap("{keyMapId}")).withRel("keyMaps"));
问题在于,当变量"{keyMapId}"到达UriTemplate构造函数时,它已包含在编码的URL中:
The problem is that by the time the variable "{keyMapId}" reaches the UriTemplate constructor, it's been included in an encoded URL:
http://localhost/api/keyMaps/%7BkeyMapId%7D
因此UriTemplate的构造函数无法将其识别为包含变量.
So UriTemplate's constructor doesn't recognise it as containing a variable.
如何说服ControllerLinkBuilder我要使用模板变量?
How can I persuade ControllerLinkBuilder that I want to use template variables?
推荐答案
从此提交开始:
https://github.com/spring-projects/spring-hateoas/commit/2daf8aabfb78b6767bf27ac3e473832c872302c7
您现在可以在需要路径变量的地方传递null
.它对我有用,没有解决方法.
You can now pass null
where path variable is expected. It works for me, without workarounds.
resource.add(linkTo(methodOn(UsersController.class).someMethod(null)).withRel("someMethod"));
结果:
"someMethod": {
"href": "http://localhost:8080/api/v1/users/{userId}",
"templated": true
},
还要检查相关问题: https://github.com/spring-projects/spring-hateoas/issues/545
这篇关于使用ControllerLinkBuilder的模板变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!