如何使用DiscoveredResource遍历RepositoryRestResource公开的单个实体资源

时间:2018-12-21 10:48:28

标签: spring-cloud spring-data-rest spring-hateoas

我正在尝试通过使用发现服务器来建立具有多个应用程序连接的系统。我可以遍历对特定资源的hal响应,但是我正在寻找一种解决方案,以将集合资源转换为单个资源,并找到特定实体的数据。

在1个应用程序中,我有一个RepositoryRestResource公开了一些对象:

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

在其他一些应用程序中,我想讲一件事。我有一个ID(假设它是1),并且具有集合和单个资源的关系名称。

我想使用DiscoveredResource来获得到该单个项目资源或集合资源的链接,然后可以使用ID(需要模板资源)以某种方式对其进行扩展。 如果可能的话,我不想只在网址末尾添加“ / 1”。

这是我当前如何创建DiscoveredResource指向集合资源的方式:

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

我也应该在@RepositoryRestResource创建的集合资源上添加模板链接。还是我还缺少其他技巧?

1 个答案:

答案 0 :(得分:0)

这里的解决方案是添加一个自定义方法作为@RestRepositoryResource(collectionResourceRel="things", itemResourceRel="thing") public interface ThingRepo extends CrudRepository<Thing,Long> { @RestResource(rel = "thing") Thing findOneById(@Param("id") Long id); } ,该方法公开与您可以遵循的模板URL的关系。

回购:

DiscoveredResource resource = new DiscoveredResource(new DynamicServiceInstanceProvider(discoveryClient, traverson -> traverson.follow("things","search","thing"));
Link link = resource.getLink().expand(id);

发现+特拉弗森:

:hover