Using Spring Data REST with JPA in version 2.0.2.RELEASE.

How can I disable Hypertext Application Language (HAL) in the JSON ? http://stateless.co/hal_specification.html

I have tried many things already, but to no avail. For example, I have set Accept and Content-type headers to "application/json" instead of "application/hal+json" but I still receive the JSON content with hyper links.

For example, I'd like to get something like:

{
"name" : "Foo",
"street" : "street Bar",
"streetNumber" : 2,
"streetLetter" : "b",
"postCode" : "D-1253",
"town" : "Munchen",
"country" : "Germany",
"phone" : "+34 4410122000",
"vat" : "000000001",
"employees" : 225,
"sector" : {
     "description" : "Marketing",
     "average profit": 545656665,
     "average employees": 75,
     "average profit per employee": 4556
     }
}

而不是:

{
"name" : "Foo",
"street" : "street Bar",
"streetNumber" : 2,
"streetLetter" : "b",
"postCode" : "D-1253",
"town" : "Munchen",
"country" : "Germany",
"phone" : "+34 4410122000",
"vat" : "000000001",
"employees" : 225,
"_links" : {
     "self" : {
          "href" : "http://localhost:8080/app/companies/1"
     },
     "sector" : {
          "href" : "http://localhost:8080/app/companies/1/sector"
     }
}
}

谢谢你的帮助.

推荐答案

(Hyper)media types

The default settings for Spring Data REST use HAL as the default hypermedia representation format, so the server will return the following for the given Accept headers:

  • No header -> application/hal+json -> HAL
  • application/hal+json -> application/hal+json -> HAL
  • application/json -> application/json -> HAL (this is what the default configures)
  • application/x-spring-data-verbose+json -> application/x-spring-data-verbose+json -> a Spring Data specific format (using links for the links container and content as wrapper for the collection items.

If you configure RepositoryRestConfiguration.setDefaultMediaType(…) to a non-HAL format, the server will return the Spring Data specific JSON format unless you explicitly ask for application/hal+json. Admittedly the configuration option is probably a bit misleading, so I filed DATAREST-294 to improve this. The issue was resolved in 2.1 RC1 (Dijkstra) 2014.

Note that we effectively need a hypermedia format in place to be able to express relations between managed resources and enable discoverability of the server. So there's no way you'll be able to get rid of it completely. This is mostly due to the fact that you could easily crash the server if you expose entities that have bidirectional relationships or make up an enormous object graph.

Inlining related entities

If you never want to have sectors linked to and always inline them, one option is to simply exclude the SectorRepository from being exported as a REST resource in the first place. You can achieve this by annotating the repository interface with @RepositoryRestResource(exported = false).

To get a representation returned as you posted in your lower example have a look at the projections feature introduced in Spring Data REST 2.1 M1. It basically allow you to craft optional views on a resource that can differ from the default one via a simple interface.

You'd basically define an interface:

@Projection(name = "foo", types = YourDomainClass.class)
interface Inlined {

  // list all other properties

  Sector getSector();
}

如果您将该接口放入域类的(子)包中,或者通过RepositoryRestConfiguration.projectionConfiguration()手动注册它,资源公开YourDomainClass将接受请求参数projection,以便在本例中传入foo将呈现您想要的内联表示.

This commit has more info on the feature in general, this commit has an example projection defined.

Json相关问答推荐

如何使用 jq 返回此 JSON 文件的“文本”字段?

使用 jq 同时迭代数组

PowerShell - 如何迭代 PSCustomObject 嵌套对象?

C# 合并 2 个几乎相同的 JSON 对象

使用 Swift 4 中的 JSONDecoder,缺少的键可以使用默认值而不是可选属性吗?

使用 axios 在 POST multipart/form-data 请求中发送文件和 json

我所有的 .json 文件在从 schemastore.azurewebsites.net 加载参考/模式时都有问题

如何解析 JSON 文件?

在 ASP.NET Core 2.0 Web Api 中返回“原始”json

JSON Jackson 将不同的键解析到同一个字段中

pandas.io.json.json_normalize 带有非常嵌套的 json

fetch() 输入意外结束

来自 JSON 字符串的 Typescript `enum`

AWS:如何修复 S3 事件用“+”登录 json 中的对象键名替换空格

在 Ubuntu 10.04 上安装 jq JSON 处理器

使用 UNION 时无法识别 json[] 类型的相等运算符

处理响应 - SyntaxError:使用模式时输入意外结束:'no-cors'

如何使用jq判断数组中是否存在元素

jq:按属性分组和键

使用 jq 向现有 JSON 数组添加新元素