在 Spring Data REST 中公开集合实体上的链接 [英] Exposing link on collection entity in spring data REST
问题描述
使用 spring data 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屋!