spring-hateoas - 我们没有通用方法来创建 rel 链接 Spring hatoas 1.1.x 中的所有控制器端点
问题描述
我希望使用 Java 反射以通用方式为我在应用程序中公开的所有控制器端点创建一个 rel 链接。不是通过使用 LinkBuilder 的“methodOn”引用来单独引用控制器及其各自的方法。我尝试了以下方法以在我的通用服务中实现相同的目标。
requestMappingHandlerMapping.getHandlerMethods().values().stream()
.map(HandlerMethod::getMethod)
.forEach(method -> {
Entry<String, String> index = retrieveKeyAnnotation(method); //Here we have a customized Key annotation on our controller class, Any Insights on resolving this issue could be much helpful and appreciated.
from which the the rel Key is formed.
if (index != null) { resourceSupport.addIfSecure(WebMvcLinkBuilder.linkTo(method,method.getParameters()).withRel(index.getKey()));
} //resourceSupport is an object of a class which inherits RepresentationModel.
});
我的控制器类看起来像这样。
@Key(value="key", doc="Description")
@GetMapping("/endPoint")
public ResponseEntity<EntityModel<ModelClass>> trigger(@RequestParam String param){
}
这里形成了 rel 链接。但是,如果我的控制器中有查询参数,则不会出现在 rel 链接中。请在下面找到示例。
预期的:
{
"_links": {
"curie-prefix:keyu": {
"href": "http://appName/contextPath/endPoint?param={param}",
},
"curies": [{
"href": "http://appName/contextPath/generated-docs/api-guide.html#resources-{rel}",
"name": "curie-prefix",
"templated": true
}]
}
}
实际价值:
"_links": {
"curie-prefix:keyu": {
"href": "http://appName/contextPath/endPoint",
},
"curies": [{
"href": "http://appName/contextPath/generated-docs/api-guide.html#resources-{rel}",
"name": "curie-prefix",
"templated": true
}]
}
}
您可以在实际链接中找到 queryParam 缺失。在这里,当我通过流程进行调试时,我发现 HierarchicalUriComponents 由于死锁条件而无法识别 QueryParam。
解决方案
推荐阅读
- python - 将两个不同列表中的值顺序分配给python数据框中的列
- visual-studio - 让 Premake 生成一个引用现有 .props 文件的 vcxproj 文件
- c - malloc:从内存中获取信息
- angular - Docker 上的 Jenkins 开始在 Docker 上构建 Angular 应用程序
- javascript - 从大括号外读取时,变量“未声明”
- authentication - 仅 OTP 身份验证
- asp.net-core - 如果客户端用户在 API 管理服务前端进行了身份验证,如何将用户详细信息发送到后端 API?
- vue.js - 如何更改禁用组件的文本颜色
- excel - 如何使用 Range.Formula 使用变量范围
- php - 为什么我在尝试计算 mysqli 结果时会收到警告?