Using Spring Data REST with JPA in version 2.0.2.RELEASE.
在版本2.0.2.RELEASE中使用Spring数据REST和JPA。
How can I disable Hypertext Application Language (HAL) in the JSON ? http://stateless.co/hal_specification.html
如何禁用JSON中的超文本应用程序语言(HAL) ?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.
我已经试过很多东西,但都没用。例如,我将Accept和content -type头设置为“application/json”,而不是“application/hal+json”,但我仍然收到带有超链接的json内容。
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
}
}
Instead of:
而不是:
{
"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"
}
}
}
Thanks for your help.
谢谢你的帮助。
1 个解决方案
#1
36
(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:
Spring Data REST的默认设置使用HAL作为默认的超媒体表示格式,因此服务器将为给定的Accept标头返回以下内容:
- No header ->
application/hal+json
-> HAL - 没有header ->应用程序/hal+json -> hal。
-
application/hal+json
->application/hal+json
-> HAL - 应用/hal+json ->应用/hal+json -> hal
-
application/json
->application/json
-> HAL (this is what the default configures) - 应用/json ->应用/json -> HAL(这是默认配置)
-
application/x-spring-data-verbose+json
->application/x-spring-data-verbose+json
-> a Spring Data specific format (usinglinks
for the links container andcontent
as wrapper for the collection items. - 应用程序/x-spring-data-verbose+json ->应用程序/x-spring-data-verbose+json ->一个特定于Spring数据的格式(使用链接容器和内容作为收集项的包装)。
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.
如果您将RepositoryRestConfiguration.setDefaultMediaType(……)配置为非hal格式,那么服务器将返回与Spring数据相关的JSON格式,除非您明确要求使用application/hal+ JSON。诚然,配置选项可能有点误导人,所以我提交了DATAREST-294来改进它。这个问题在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)
.
如果您不想让扇区链接到并且始终内联它们,那么一种选择就是将SectorRepository从一开始就导出为REST资源。您可以通过使用@RepositoryRestResource(export = 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.
要获得在较低示例中发布的结果,请查看Spring Data REST 2.1 M1中引入的投影特性。它基本上允许您在资源上创建可选的视图,这些视图可以通过一个简单的接口与默认的视图不同。
You'd basically define an interface:
你基本上会定义一个接口:
@Projection(name = "foo", types = YourDomainClass.class)
interface Inlined {
// list all other properties
Sector getSector();
}
If you either put this interface into a (sub)package of your domain class or manually register it via RepositoryRestConfiguration.projectionConfiguration()
the resources exposing YourDomainClass
will accept a request parameter projection
so that passing in foo
in this example would render the inlined representation as you want it.
如果您将此接口放入域类的(子)包中,或者通过RepositoryRestConfiguration.projectionConfiguration()手动注册该接口,那么公开您的domainclass的资源将接受请求参数投影,以便在本例中传入foo时呈现所需的内联表示。
This commit has more info on the feature in general, this commit has an example projection defined.
这个提交具有关于这个特性的更多信息,这个提交定义了一个示例投影。
#1
36
(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:
Spring Data REST的默认设置使用HAL作为默认的超媒体表示格式,因此服务器将为给定的Accept标头返回以下内容:
- No header ->
application/hal+json
-> HAL - 没有header ->应用程序/hal+json -> hal。
-
application/hal+json
->application/hal+json
-> HAL - 应用/hal+json ->应用/hal+json -> hal
-
application/json
->application/json
-> HAL (this is what the default configures) - 应用/json ->应用/json -> HAL(这是默认配置)
-
application/x-spring-data-verbose+json
->application/x-spring-data-verbose+json
-> a Spring Data specific format (usinglinks
for the links container andcontent
as wrapper for the collection items. - 应用程序/x-spring-data-verbose+json ->应用程序/x-spring-data-verbose+json ->一个特定于Spring数据的格式(使用链接容器和内容作为收集项的包装)。
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.
如果您将RepositoryRestConfiguration.setDefaultMediaType(……)配置为非hal格式,那么服务器将返回与Spring数据相关的JSON格式,除非您明确要求使用application/hal+ JSON。诚然,配置选项可能有点误导人,所以我提交了DATAREST-294来改进它。这个问题在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)
.
如果您不想让扇区链接到并且始终内联它们,那么一种选择就是将SectorRepository从一开始就导出为REST资源。您可以通过使用@RepositoryRestResource(export = 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.
要获得在较低示例中发布的结果,请查看Spring Data REST 2.1 M1中引入的投影特性。它基本上允许您在资源上创建可选的视图,这些视图可以通过一个简单的接口与默认的视图不同。
You'd basically define an interface:
你基本上会定义一个接口:
@Projection(name = "foo", types = YourDomainClass.class)
interface Inlined {
// list all other properties
Sector getSector();
}
If you either put this interface into a (sub)package of your domain class or manually register it via RepositoryRestConfiguration.projectionConfiguration()
the resources exposing YourDomainClass
will accept a request parameter projection
so that passing in foo
in this example would render the inlined representation as you want it.
如果您将此接口放入域类的(子)包中,或者通过RepositoryRestConfiguration.projectionConfiguration()手动注册该接口,那么公开您的domainclass的资源将接受请求参数投影,以便在本例中传入foo时呈现所需的内联表示。
This commit has more info on the feature in general, this commit has an example projection defined.
这个提交具有关于这个特性的更多信息,这个提交定义了一个示例投影。