如何使用DiscoveredResource穿越到由RepositoryRestResource暴露一个单一的实体资源

问题描述 投票:0回答:1

我试图建立一个系统,与多个应用程序连接通过使用发现服务器。我可以遍历HAL提出的是一个特定的资源,但我正在寻找一个解决方案,从收集的资源得到一个单一的资源,并找到数据特定实体。

在1个应用程序我有一个RepositoryRestResource暴露一些对象:

@RestRepositoryResource(collectionResourceRel="things", itemResourceRel="thing") public interface ThingRepo extends CrudRepository<Thing,Long> {}

在其他一些应用程序,我想获得到一个单一的东西。我有ID(让我们说这是1),并有集合的关系名称和单一的资源。

我想用一个DiscoveredResource获得连结此单项资源,或收集资源,我可以然后以某种方式扩大使用ID(这可能需要一个模板资源)。如果可能的话我不希望只是添加“/ 1”在URL的结尾。

这是怎么了我目前创建DiscoveredResource指向集合资源:

new DiscoveredResource(new DynamicServiceInstanceProvider(discoveryClient, traverson -> traverson.follow("things"));

我是有可能要增加在由@RepositoryRestResource创建的集合资源模板链接。或者是有一些其他的把戏我失踪?

spring-cloud spring-data-rest spring-hateoas
1个回答
0
投票

这里的解决方案是增加一个自定义的方法,它公开了模板URL,那么你可以遵循的关系的@RestResource

回购:

@RestRepositoryResource(collectionResourceRel="things", itemResourceRel="thing") public interface ThingRepo extends CrudRepository<Thing,Long> {

    @RestResource(rel = "thing")
    Thing findOneById(@Param("id") Long id);
}

发现+ traverson:

DiscoveredResource resource = new DiscoveredResource(new DynamicServiceInstanceProvider(discoveryClient, traverson -> traverson.follow("things","search","thing"));
Link link = resource.getLink().expand(id);
© www.soinside.com 2019 - 2024. All rights reserved.