所以假设我有一个现有的应用程序,它有两个端点/people 和/pants。调用 GET/people 返回:
[
{
"name":"john",
"age":37,
"pants":[
{
"color":"green",
"brand":"levis",
"size":"medium"
},
{
"color":"indigo",
"brand":"jncos",
"size":"medium-with-huge-legs"
}
]
},
{
"name":"june",
"age":23,
"pants":[
{
"color":"pink",
"brand":"gap",
"size":"small"
}
]
}
]
如果我要使用 Spring Data Rest 并调用 GET/person 我会收到类似的信息:{
"_links":{
"next":{
"href":"http://myapp.com/people?page=1&size=20"
},
"self":{
"href":"http://myapp.com/people{&page,size,sort}",
"templated":true
},
"search":{
"href":"http://myapp.com/people/search"
}
},
"_embedded":{
"people":[
{
"name":"john",
"age":37,
"_links":{
"self":{
"href":"http://myapp.com/people/john"
},
"pants":{
"href":"http://myapp.com/people/john/pants"
}
}
},
{
"name":"june",
"age":23,
"_links":{
"self":{
"href":"http://myapp.com/people/june"
},
"pants":{
"href":"http://myapp.com/people/june/pants"
}
}
}
]
}
}
假设我有一堆我不想更改的现有客户端 - 有没有办法在某些情况下禁用响应的超媒体部分(比如 Accept="application/json")但启用它们其他(接受=“hal+json”)?谢谢!
更新
好的 - 所以让我懊恼的是,我想要做的事情不受支持。我理解为什么 Spring Data Rest 强烈倾向于超媒体……但我不认为提供基于 header “禁用”超媒体的能力,从而提供更多选项是一件坏事。
除此之外,我有点不确定如何通过我自己的 Controller 实际实现这一点。如果我创建一个 Controller 并尝试覆盖
/people
请求映射 produces = "application/json" I am able to get the "raw" json back with Accept="application/json" but if I pass
Accept="application/hal+json"` 我得到一个带有“找不到可接受的表示”的 406。看起来 SDR 资源映射没有与内容类型映射......有什么建议吗?
最佳答案
简短的回答是,你不能在没有仇恨的情况下使用 spring-data-rest。如果你想在没有仇恨的情况下构建你的 web 服务,你必须编写自己的 Controller (它仍然可以使用 spring-data 存储库)。
在 this SO post 中引用 Oliver Gierke :
Actually my whole point is: the server is just doing decent REST. If that breaks the client, it's the client that needs to be fixed (tweaked). So the hypermedia aspect is a fundamental one to Spring Data REST and we're not going to back out of that. That's probably not satisfying in your concrete situation but should answer the question at least :). – Oliver Gierke
关于rest - 可选择在 Spring Data Rest 中禁用 HATEOAS 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27070930/