在版本2.0.2.RELEASE中使用带有JPA的Spring Data REST.
如何在JSON中禁用超文本应用程序语言(HAL)?http://stateless.co/hal_specification.html
我已经尝试了很多东西,但无济于事.例如,我已将Accept和Content-type标头设置为"application/json"而不是"application/hal + json",但我仍然收到带有超链接的JSON内容.
例如,我想得到类似的东西:
{
"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
}
}
Run Code Online (Sandbox Code Playgroud)
代替:
{
"name" : "Foo",
"street" : "street Bar",
"streetNumber" : 2,
"streetLetter" : "b",
"postCode" : "D-1253",
"town" …Run Code Online (Sandbox Code Playgroud) 目前spring-data-rest正在spring-boot我的项目中以HAL格式返回JSON .我正在使用ember.js前端,并希望使用jsonapi(http://jsonapi.org/)规范.
我如何注册一个新的JSON格式化策略,因为我需要自己编写格式化程序,因为它还不存在?
当我为我的实体使用 Spring Data Rest 提供的默认控制器时,一切都会正常工作。输出如下所示:
{
"_links" : {
"search" : {
"href" : "http://localhost:8080/users/search"
}
},
"_embedded" : {
"users" : [ {
"firstName" : "Max",
"lastName" : "Mustermann",
"email" : "mail@max-mustermann.de",
"_links" : {
"self" : {
"href" : "http://localhost:8080/users/myadmin"
}
}
} ]
}
}
Run Code Online (Sandbox Code Playgroud)
但如果我使用自己的控制器,输出将如下所示:
[ {
"firstName" : "Max",
"lastName" : "Mustermann",
"email" : "mail@max-mustermann.de",
"links" : [ {
"rel" : "self",
"href" : "http://localhost:8080/user/myadmin"
} ]
} ]
Run Code Online (Sandbox Code Playgroud)
我的控制器
@RestController
@RequestMapping("/user")
@EnableHypermediaSupport(type = {HypermediaType.HAL}) …Run Code Online (Sandbox Code Playgroud) 我是spring-data-rest的新手。在我的应用程序中,当我进行一次休息呼叫时,我得到的json包含自我链接和与外键表的链接。但是我想用json代替链接。我使用telosys工具生成器生成了代码。这是我对数据库中的“商品”表进行REST调用时所得到的JSON:
{
"id" : 1,
"rate": 300,
"type": "item",
"shortDescription": "test",
"longDescription": "test test",
"_links": {
"self": {
"href": "http://localhost:8080/sportsrest/merchandises/1"
},
"listOfMerchandiseAttribute": {
"href": "http://localhost:8080/sportsrest/merchandises/1/listOfMerchandiseAttribute"
},
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我不是要获取“ listOfMerchandiseAttribute”的链接,而是要获取listOfMerchandiseAttribute的JSON。listOfMerchandiseAttribute是我数据库中的另一个表
那就是我想要这样的json:
{
"id": 1,
"rate": 300,
"type": "item",
"shortDescription": "test",
"longDescription": "test test",
"_links": {
"self": {
"href": "http://localhost:8080/sportsrest/merchandises/1"
},
"listOfMerchandiseAttribute": {
"id": 1,
"attributeName": "testname",
"attributeValue": 50
},
}
}
Run Code Online (Sandbox Code Playgroud)
当我在google上搜索时,我得到了一些结果,并根据该结果更改了ApplicationConfig.java文件,但仍在获取链接而不是JSON。这是我的ApplicationConfig文件。
ApplicationConfig.java
/*
* Created on 19 Mar 2015 ( Time 14:41:07 )
* Generated by Telosys Tools Generator …Run Code Online (Sandbox Code Playgroud)