【发布时间】:2023-04-09 13:40:01
【问题描述】:
如何配置 Spring Data REST 以删除 Repository 接口端点 的 Collection 资源响应上的实体关联链接(仅留下“self”),无需设置 exported= @ResResource 注释上的 false(需要保持导出端点)
我们的实体中 _links 部分在响应中具有最大尺寸:
打开Item resource _ 链接对于浏览关联非常有用。
但在 Collection Resources 上,主要是大型集合,这些信息并不重要,而且会使响应变得不必要。
我们需要更改此响应:
{
"_embedded" : {
"persons" : [ {
"id" : "bat_3191",
"name" : "B",
"_links" : { // 80% of response size !!
"self" : {
"href" : "http://localhost:8080/api/persons/bat_3191"
},
"orders" : {
"href" : "http://localhost:8080/api/persons/bat_3191/order"
},
"payments" : {
"href" : "http://localhost:8080/api/persons/bat_3191/payments"
},
"childrens" : {
"href" : "http://localhost:8080/api/persons/bat_3191/childrens"
},
"invoices" : {
"href" : "http://localhost:8080/api/persons/bat_3191/invoices"
},
"brands" : {
"href" : "http://localhost:8080/api/persons/bat_3191/brands"
},
}
},
{ person [2] }
...
{ person [N] }
]
},
"_links" : {
[page links]
}
对于 _links 部分的唯一“自我”:
{
"_embedded" : {
"persons" : [ {
"id" : "bat_3191",
"name" : "B",
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/persons/bat_3191"
}
}
}, {
"id" : "bat_2340",
"name" : "B",
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/persons/bat_2340"
}
}
【问题讨论】:
-
很抱歉,如果我没有解释清楚我的问题,但建议的内容不重复。我们需要删除所有实体的 JSON 的 _link 部分的关系,以减少收集请求的响应大小。我实现了重构 PersistentEntityJackson2Module 以避免杰克逊对链接进行序列化,但我相信这将是一个更好的方法。我不知道如何使用 RestRespositoryMvcConfiguration 来修改这方面。
标签: spring spring-data-rest spring-hateoas jackson2