【问题标题】:How to use DiscoveredResource to traverse to a single entity resource exposed by a RepositoryRestResource如何使用 DiscoveredResource 遍历 RepositoryRestResource 暴露的单个实体资源
【发布时间】:2018-12-21 10:48:27
【问题描述】:

我正在尝试设置一个系统,其中包含多个使用发现服务器连接的应用程序。我可以遍历对特定资源的 hal 响应,但我正在寻找一种解决方案来从集合资源获取单个资源并找到特定实体的数据。

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

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

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

我想使用 DiscoveredResource 来获取指向此单个项目资源或集合资源的链接,然后我可以使用 ID 以某种方式扩展它(这将需要模板化资源)。 如果可能的话,我不想只在 URL 的末尾添加“/1”。

这是我目前创建 DiscoveredResource 以指向集合资源的方式:

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

我应该并且是否可以在由@RepositoryRestResource 创建的集合资源上添加模板链接。还是我缺少其他一些技巧?

【问题讨论】:

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


    【解决方案1】:

    这里的解决方案是添加一个自定义方法作为 @RestResource,它公开与模板 URL 的关系,然后您可以遵循。

    回购:

    @RestRepositoryResource(collectionResourceRel="things", itemResourceRel="thing") public interface ThingRepo extends CrudRepository<Thing,Long> {
    
        @RestResource(rel = "thing")
        Thing findOneById(@Param("id") Long id);
    }
    

    发现+遍历:

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

    【讨论】:

      猜你喜欢
      • 2015-03-15
      • 2019-05-25
      • 2010-10-09
      • 2011-09-19
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 2010-10-13
      相关资源
      最近更新 更多