基本路径未出现在ResourceProcessor自定义链接中

在Spring Data REST中,我使用ResourceProcessor创建自定义链接:

 @Component public class ServiceInstanceProcessor implements ResourceProcessor<Resource> { @Override public Resource process(Resource 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 ,即使默认链接包含基本路径:

 { ... "_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" }, ... } 

}

我还需要做些什么才能让基本路径出现在链接中吗?

我认为它与bug有关ControllerLinkBuilder没有考虑Spring Data REST的基本路径,而且HAL中没有显示自定义控制器+更改的基本路径

作为解决方法,我接下来做:

 @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相同:

 resources.add(fixLinkSelf(methodOn(VoteController.class).history())); resources.add(fixLinkTo(methodOn(VoteController.class).current()).withRel("current")); 

基于addPath当前请求的简单案例的其他解决方法:

 new Link(ServletUriComponentsBuilder.fromCurrentRequest().path(addPath).build().toUriString())