【问题标题】:Exposing link on collection entity using spring hateoas使用 spring hatoas 暴露集合实体上的链接
【发布时间】:2017-03-02 13:41:54
【问题描述】:

我的问题与这里 (Exposing link on collection entity in spring data REST) 提出的问题完全相同。但是该主题中的任何内容都无法帮助我将自定义链接添加到集合调用。

@Component
public class EventListResourceProcessor implements ResourceProcessor<Resources<Event>> {

    @Autowired
    private RepositoryEntityLinks entityLinks;

    @Override
    public Resources<Event> process(Resources<Event> events) {
        events.add(entityLinks.linkToCollectionResource(Event.class).withRel("events"));
        return events;
    }
}

在这种情况下永远不会调用 process 方法。

我需要调用 http://localhost:8080/event 并在 _links 部分下使用 my_custom_link 获取以下 JSON:

{
  "_embedded": {
    "event": [
      {
        "id": "1",
        "name": "Updated event"
      }]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/event"
    },
    "profile": {
      "href": "http://localhost:8080/profile/event"
    },
    "my_custom_link": {
      "href": "http://localhost:8080/custom/"
    }
  },
  "page": {
    "size": 20,
    "totalElements": 4,
    "totalPages": 1,
    "number": 0
  }
}

}

你能告诉我吗?

提前致谢!

【问题讨论】:

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


    【解决方案1】:

    我的情况与您的问题类似:我已阅读您链接的问题/答案,发现没有一个可以解决问题。这是我的问题的最终答案:

    @Component
    public class MyResourceProcessor implements ResourceProcessor<Resource<MyInterface>> {
    
        @Autowired
        private EntityLinks entityLinks;
    
        @Override
        public Resource<MyInterface> process(Resource<MyInterface> event) {
            event.add(entityLinks.linkForSingleResource(MyClass.class, event.getContent().getId()).slash("custom").withRel("custom"));
            return event;
        }
    }
    

    为正在返回的相关资源集合中的每个资源调用此process 方法。 MyInterface 接口和 MyClass 类应该替换为您最终需要的任何内容,但必须以这种方式编写才能使其正常工作。以下是我用来正确调用process 方法并确定MyInterface 类型的步骤。

    1. 我创建了一个process 方法,它只是将ResourceSupport 作为参数。我在代码中创建了一个断点,并检查了扩展ResourceSupport 的底层类。就我而言,它是PersistentEntityResource。这解释了为什么使用Resource&lt;MyClass&gt;Resources&lt;MyClass&gt; 永远不会导致process 方法被调用:PersistentEntityResource 扩展Resource&lt;Object&gt;

    2. 我更新了process 方法,将PersistentEntityResource 作为参数。这导致调用 process 方法的次数超出了我的预期更改。我再次使用断点检查PersistentEntityResource 对象,目的是发现可以在它扩展的Resource&lt;Object&gt; 中找到什么类。我发现它是一个 Proxy 类,并且无法按照我的意愿强制转换为 MyClass

    3. 我按照此处找到的答案来了解有关 Proxy 类的更多信息:https://stackoverflow.com/a/3344386/1417690。在调试时,我发现了帮助定义此类的接口列表。其中之一是MyProjectionInterface 类型。我现在知道我不能使用Resource&lt;Portal&gt; 的原因是因为它实际上是Resource&lt;MyProjectionInterface&gt;

    4. 我需要处理三个不同的Projections。我没有创建三个单独的ResourcePorcoessors,而是创建了MyInterface,并让我的所有三个projection 接口都对其进行了扩展。 MyInterface 只包含一个Long getId() 方法,无论如何所有projections 都已经支持。

    5. 我更新了我的代码以使用Resource&lt;MyInterface&gt;,并使用MyClass(所有projections 都与之相关)添加了linkForSingleResource,以及我在MyInterface 中定义的getId() 方法。这成功地将所需的链接添加到要返回的每个资源。

    希望这些步骤可以帮助其他人发现如何确定将哪种类型用作process 方法的参数。

    【讨论】:

      【解决方案2】:

      因为我遇到了同样的问题,我希望在这里得到答案。解决方案是声明一个 bean ResourceProcessor 实现扩展 ResourceSupport 的正确泛型类型。

      在我们的可分页资源的例子中,右边的ResourceSupportPagedResources&lt;Resource&lt;MyInterface&gt;&gt;,如下示例:

      @Bean
      public ResourceProcessor<PagedResources<Resource<Supplier>>> pageableSupplierProcessor() {
          return new ResourceProcessor<PagedResources<Resource<Supplier>>>() {
              @Override
              public PagedResources<Resource<Supplier>> process(PagedResources<Resource<Supplier>> resources) {
                  Method registerMethod;
                  try {
                      registerMethod = SupplierRegistrationController.class.getMethod("register",
                              RegistrationRequest.class);
                      Link registerLink = linkTo(registerMethod, new RegistrationRequest()).withRel("register");
                      resources.add(registerLink);
                      return resources;
                  } catch (NoSuchMethodException | SecurityException e) {
                      throw new IllegalStateException(e);
                  }
              }
          };
      }
      

      【讨论】:

      • 在目前的最新版本中,您现在可以使用:RepresentationModelProcessor&lt;PagedModel&lt;EntityModel&lt;T&gt;&gt;&gt;。 HTH 别人。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      • 2010-10-13
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多