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

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

问题描述

在2.0.2.RELEASE版本中将Spring Data REST与JPA结合使用.

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"
     }
}
}

感谢您的帮助.

推荐答案

(超级)媒体类型

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

(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:

  • 没有标题-> 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格式,则除非您明确要求application/hal+json,否则服务器将返回特定于Spring Data的JSON格式.诚然,配置选项可能有点误导,所以我提出了 DATAREST-294 来改善这一点.该问题已在2014年的RC1(Dijkstra)中得到解决.

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

要获得在下例中发布的表示形式,请查看投影 Spring Data REST 2.1 M1中引入的a>功能.基本上,您可以通过简单的界面在与默认资源不同的资源上制作可选视图.

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天全站免登陆