在 JSON 中禁用超文本应用程序语言 (HAL)? [英] Disable Hypertext Application Language (HAL) in JSON?

查看:17
本文介绍了在 JSON 中禁用超文本应用程序语言 (HAL)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 2.0.2.RELEASE 版本中使用带有 JPA 的 Spring Data REST.

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

如何在 JSON 中禁用超文本应用程序语言 (HAL)?http://stateless.co/hal_specification.html

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

我已经尝试了很多东西,但都无济于事.例如,我已将 Accept 和 Content-type 标头设置为application/json"而不是application/hal+json",但我仍然收到带有超链接的 JSON 内容.

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

Spring Data REST 的默认设置使用 HAL 作为默认超媒体表示格式,因此服务器将为给定的 Accept 标头返回以下内容:

  • 无标题 -> application/hal+json -> HAL
  • application/hal+json -> application/hal+json -> HAL
  • application/json -> application/json -> HAL(这是默认配置的)
  • application/x-spring-data-verbose+json -> application/x-spring-data-verbose+json -> Spring Data 特定格式(使用links 用于链接容器,content 作为集合项的包装器.
  • 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.

如果您将 RepositoryRestConfiguration.setDefaultMediaType(…) 配置为非 HAL 格式,服务器将返回 Spring Data 特定的 JSON 格式,除非您明确要求 application/hal+json.诚然,配置选项可能有点误导,所以我提交了 DATAREST-294 来改进这个.该问题已在 2.1 RC1 (Dijkstra) 2014 中得到解决.

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.

如果您永远不想将扇区链接到并始终内联它们,一个选择是首先将 SectorRepository 排除在作为 REST 资源导出之外.您可以通过使用 @RepositoryRestResource(exported = false) 注释存储库接口来实现这一点.

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).

要获得在下例中发布的表示返回,请查看 projections Spring Data REST 2.1 M1 中引入的功能.它基本上允许您通过一个简单的界面来制作与默认资源不同的资源的可选视图.

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 将按照您的需要呈现内联表示.

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.

此提交提供了有关该功能的更多信息, 此提交定义了一个示例投影.

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

这篇关于在 JSON 中禁用超文本应用程序语言 (HAL)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆