首页 > 解决方案 > 我们没有通用方法来创建 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。

标签: spring-hateoas

解决方案


推荐阅读