关于 Hateoas + SpringBoot + Kotlin 的一些说明(结构化响应)
Some clarification on Hateoas + SpringBoot + Kotlin (structuring responses)
据我所知,Spring Hateoas 支持两种(或至少两种)格式:collection+json
& hal
我正在尝试使用 HAL
格式(我认为),但我无法正确呈现我的元素之一
函数1:(Returns单Resource
)
@GetMapping("/test2/{array_index}")
fun test2(@PathVariable array_index: Int): ResponseEntity<Resource<SDDFService>> {
val service = SDDFService(globals.advertisements[array_index])
return ResponseEntity(Resource(service), HttpStatus.OK)
}
本次调用的JSON
return是:
{
"content": {
"serviceID": "FMS",
"peerID": "FlightSim_0977",
"peerName": "FlightSim",
"translator": "SDDFTranslator:AdvTranslator",
"transporter": "SDDFTransporter:CMSVTransporter#trumpet.mitre.org@41375",
"contentIterator": [
{
"comment": "",
"units": "",
"type": "STRING",
"key": "fms",
"nestedContentSet": null
}
]
},
"_links": {
"peer_id": {
"href": "http://localhost:8090/test/peer_id/FlightSim_0977"
},
"peer_name": {
"href": "http://localhost:8090/test/peer_name/FlightSim"
},
"service_name": {
"href": "http://localhost:8090/test/service/FMS"
}
}
}
我喜欢这个因为我们的根是 content
并且我们有 HAL 风格 _links
版本 2 - 多个 returns
我的问题是当我开始 returning collection 事情变得有点古怪:
@GetMapping("/test3/{array_index}")
fun test3(@PathVariable array_index: Int): ResponseEntity<Resources<SDDFService>> {
val service1 = SDDFService(globals.advertisements[array_index])
val service2 = SDDFService(globals.advertisements[array_index+1])
return ResponseEntity(
Resources(listOf(service1, service2)),
HttpStatus.OK
)
}
```
我得到的 return 看起来像:
{
"_embedded": {
"sDDFServiceList": [
{
"content": {
"serviceID": "FMS",
"peerID": "FlightSim_0977",
"peerName": "FlightSim",
"translator": "SDDFTranslator:AdvTranslator",
"transporter": "SDDFTransporter:CMSVTransporter#trumpet.mitre.org@41375",
"contentIterator": [
.etc
我的问题是 - 有没有办法更改 sDDFServiceList
的名称。我尝试了各种注释,但似乎没有任何改变(除了更改 class 名称本身)
Class默认值
@JsonRootName("ROOT")
@JsonTypeName("typeName")
data class SDDFService @JsonCreator
constructor(@JsonProperty("content") val advertisement: SDDFAdvertisement) : ResourceSupport() {
init {
add(linkTo(methodOn(TestController::class.java).peerID(advertisement.peerID.toString())).withRel("peer_id"))
add(linkTo(methodOn(TestController::class.java).peerName(advertisement.peerName.toString())).withRel("peer_name"))
add(linkTo(methodOn(TestController::class.java).getByServiceName(advertisement.serviceID)).withRel("service_name"))
}
}
我几乎遍历了每一个堆栈 post 我可以找到并尝试很多不同的东西 - 但就目前而言,我能想到的就是改变我的 class 名字 -它仍然会在它的末尾附加 List
。
为什么我不明白为什么单个 Resource
得到一个 content
字段而 resources
object 似乎同时得到 _embedded
和<className>List
字段
您是否尝试在 class 本身上添加关系注释?喜欢:
import org.springframework.hateoas.core.Relation;
@Relation(collectionRelation = "services")
public class SDDFService extends ResourceSupport {
}
据我所知,Spring Hateoas 支持两种(或至少两种)格式:collection+json
& hal
我正在尝试使用 HAL
格式(我认为),但我无法正确呈现我的元素之一
函数1:(Returns单Resource
)
@GetMapping("/test2/{array_index}")
fun test2(@PathVariable array_index: Int): ResponseEntity<Resource<SDDFService>> {
val service = SDDFService(globals.advertisements[array_index])
return ResponseEntity(Resource(service), HttpStatus.OK)
}
本次调用的JSON
return是:
{
"content": {
"serviceID": "FMS",
"peerID": "FlightSim_0977",
"peerName": "FlightSim",
"translator": "SDDFTranslator:AdvTranslator",
"transporter": "SDDFTransporter:CMSVTransporter#trumpet.mitre.org@41375",
"contentIterator": [
{
"comment": "",
"units": "",
"type": "STRING",
"key": "fms",
"nestedContentSet": null
}
]
},
"_links": {
"peer_id": {
"href": "http://localhost:8090/test/peer_id/FlightSim_0977"
},
"peer_name": {
"href": "http://localhost:8090/test/peer_name/FlightSim"
},
"service_name": {
"href": "http://localhost:8090/test/service/FMS"
}
}
}
我喜欢这个因为我们的根是 content
并且我们有 HAL 风格 _links
版本 2 - 多个 returns
我的问题是当我开始 returning collection 事情变得有点古怪:
@GetMapping("/test3/{array_index}")
fun test3(@PathVariable array_index: Int): ResponseEntity<Resources<SDDFService>> {
val service1 = SDDFService(globals.advertisements[array_index])
val service2 = SDDFService(globals.advertisements[array_index+1])
return ResponseEntity(
Resources(listOf(service1, service2)),
HttpStatus.OK
)
}
```
我得到的 return 看起来像:
{
"_embedded": {
"sDDFServiceList": [
{
"content": {
"serviceID": "FMS",
"peerID": "FlightSim_0977",
"peerName": "FlightSim",
"translator": "SDDFTranslator:AdvTranslator",
"transporter": "SDDFTransporter:CMSVTransporter#trumpet.mitre.org@41375",
"contentIterator": [
.etc
我的问题是 - 有没有办法更改 sDDFServiceList
的名称。我尝试了各种注释,但似乎没有任何改变(除了更改 class 名称本身)
Class默认值
@JsonRootName("ROOT")
@JsonTypeName("typeName")
data class SDDFService @JsonCreator
constructor(@JsonProperty("content") val advertisement: SDDFAdvertisement) : ResourceSupport() {
init {
add(linkTo(methodOn(TestController::class.java).peerID(advertisement.peerID.toString())).withRel("peer_id"))
add(linkTo(methodOn(TestController::class.java).peerName(advertisement.peerName.toString())).withRel("peer_name"))
add(linkTo(methodOn(TestController::class.java).getByServiceName(advertisement.serviceID)).withRel("service_name"))
}
}
我几乎遍历了每一个堆栈 post 我可以找到并尝试很多不同的东西 - 但就目前而言,我能想到的就是改变我的 class 名字 -它仍然会在它的末尾附加 List
。
为什么我不明白为什么单个 Resource
得到一个 content
字段而 resources
object 似乎同时得到 _embedded
和<className>List
字段
您是否尝试在 class 本身上添加关系注释?喜欢:
import org.springframework.hateoas.core.Relation;
@Relation(collectionRelation = "services")
public class SDDFService extends ResourceSupport {
}