基本路径未出现在ResourceProcessor自定义链接中 [英] Base path doesn't appear in ResourceProcessor custom links
问题描述
在Spring Data REST中,我使用 ResourceProcessor
创建自定义链接:
In Spring Data REST I'm creating custom links using a ResourceProcessor
:
@Component
public class ServiceInstanceProcessor
implements ResourceProcessor<Resource<ServiceInstance>> {
@Override
public Resource<ServiceInstance> process(Resource<ServiceInstance> resource) {
Long id = resource.getContent().getId();
ServiceInstanceController controller =
methodOn(ServiceInstanceController.class);
resource.add(linkTo(controller.getNodeSummary(id))
.withRel("nodeSummary"));
resource.add(linkTo(controller.getHealthBreakdown(id))
.withRel("healthBreakdown"));
resource.add(linkTo(controller.getRotationBreakdown(id))
.withRel("rotationBreakdown"));
return resource;
}
}
但是生成的链接不包括基本路径,即使我已将控制器标记为 @BasePathAwareController
,即使默认链接包含基本路径:
However the generated links don't include the base path, even though I've marked the controller as @BasePathAwareController
and even though the default links do include the base path:
{
...
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/serviceInstances/101"
},
"serviceInstance" : {
"href" : "http://localhost:8080/api/serviceInstances/101{?projection}",
"templated" : true
},
"nodeSummary" : {
"href" : "http://localhost:8080/serviceInstances/101/nodeSummary"
},
"healthBreakdown" : {
"href" : "http://localhost:8080/serviceInstances/101/healthBreakdown"
},
"rotationBreakdown" : {
"href" : "http://localhost:8080/serviceInstances/101/rotationBreakdown"
},
...
}
}
是否有我需要做些什么才能让基本路径出现在链接中?
Is there anything else I need to do to get the base path to appear in the links?
推荐答案
我想它与bug有关< a href =https://github.com/spring-projects/spring-hateoas/issues/434 =nofollow noreferrer> ControllerLinkBuilder不会将Spring Data REST的基本路径考虑在内,而且自定义控制器+更改的基本路径未显示在HAL中
作为解决方法我接下来做:
As workaround I do next:
@Autowired
private final RepositoryRestConfiguration config;
private Link fixLinkSelf(Object invocationValue) {
return fixLinkTo(invocationValue).withSelfRel();
}
@SneakyThrows
private Link fixLinkTo(Object invocationValue) {
UriComponentsBuilder uriComponentsBuilder = linkTo(invocationValue).toUriComponentsBuilder();
URL url = new URL(uriComponentsBuilder.toUriString());
uriComponentsBuilder.replacePath(config.getBasePath() + url.getPath());
return new Link(uriComponentsBuilder.toUriString());
}
用法与 linkTo $ c相同$ c>:
resources.add(fixLinkSelf(methodOn(VoteController.class).history()));
resources.add(fixLinkTo(methodOn(VoteController.class).current()).withRel("current"));
基于addPath当前请求的简单案例的其他解决方法:
Other workaround for simple case based on current request with addPath:
new Link(ServletUriComponentsBuilder.fromCurrentRequest().path(addPath).build().toUriString())
这篇关于基本路径未出现在ResourceProcessor自定义链接中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!