在 Spring Data REST 中公开集合实体的链接 [英] Exposing link on collection entity in spring data REST

查看:37
本文介绍了在 Spring Data REST 中公开集合实体的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 spring 数据 REST 我公开了一个 ProjectRepository,它支持列出项目并对它们执行 CRUD 操作.当我转到 http://localhost:8080/projects/ 时,我得到了预期的项目列表.

Using spring data REST I have exposed a ProjectRepository that supports listing projects and performing CRUD operations on them. When I go to http://localhost:8080/projects/ I get the list of projects as I expect.

我想要做的是向项目集合的 JSON 响应的 _links 部分添加自定义操作.

What I am trying to do is add a custom action to the _links section of the JSON response for the Project Collection.

例如,我希望对 http://localhost:8080/projects/ 的调用返回如下内容:

For example, I'd like the call to http://localhost:8080/projects/ to return something like this:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/projects/{?page,size,sort}",
      "templated" : true
    },
    "search" : {
      "href" : "http://localhost:8080/projects/search"
    },
    "customAction" : {
       "href" : "http://localhost:8080/projects/customAction"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 0,
    "totalPages" : 0,
    "number" : 0
  }
}

其中 customAction 是在某个控制器中定义的.

Where customAction is defined in some controller.

我尝试创建以下类:

public class ProjectCollectionResourceProcessor implements ResourceProcessor<Resource<Collection<Project>>> {

    @Override
    public Resource<Collection<Project>> process(Resource<Collection<Project>> listResource) {
        // code to add the links to customAction here
        return listResource;
    }

}

并将以下 Bean 添加到我的应用程序配置中:

and adding adding the following Bean to my applications configuration:

@Bean
public ProjectCollectionResourceProcessor projectCollectionResourceProcessor() {
    return new ProjectCollectionResourceProcessor();
}

但是 process(...) 似乎从来没有被调用过.将链接添加到资源集合的正确方法是什么?

But process(...) doesn't ever seem to get called. What is the correct way to add links to Collections of resources?

推荐答案

集合资源呈现Resources>的实例,而不是Resource>.因此,如果您相应地更改 ResourceProcessor 实现中的通用类型,它应该会按您的预期工作.

The collection resources render an instance of Resources<Resource<Project>>, not Resource<Collection<Project>>. So if you change the generic typing in your ResourceProcessor implementation accordingly that should work as you expect it.

这篇关于在 Spring Data REST 中公开集合实体的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆