rest - 可选择在 Spring Data Rest 中禁用 HATEOAS 格式

标签 rest spring-data spring-data-rest hateoas hypermedia

所以假设我有一个现有的应用程序,它有两个端点/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/

相关文章:

java - Ajax + Spring Boot/Spring MVC/Jackson + 数据被截断?

java - 通过web服务和java项目上传文件到postgresql db

rest - 将文件管理到 modules/Sails.js

spring-security - Spring 数据 REST : Multiple endpoints for one entity

java - Spring Data Rest - 存储库继承创建奇怪的搜索端点

spring-boot - 将自定义 header 添加到 REST PUT 请求

java - Spring Data REST - 在不创建/搜索/findAll URL 的情况下覆盖存储库 findAll

java - 如何使用 jersey 在 java 中发送嵌套 json POST 请求?

java - Thread.sleep 是否可以减少读取和保存大文件时的内存问题

Spring 数据 JPA 和 Spring 数据 Elasticsearch ;找不到类型的属性索引?