关于Hateoas + SpringBoot + Kotlin的一些澄清(结构化响应)

时间:2018-03-16 15:06:18

标签: spring-boot jackson spring-hateoas

据我所知,Spring Hateoas支持两种(或至少两种)格式:collection+json& hal

我尝试使用HAL格式(我认为),但我无法正确渲染其中一个元素

功能1 :(返回单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为:

{
  "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 - 多次返回

我的问题是,当我开始返回一个集合时,事情变得有点时髦:

@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
    )
}

```

我得到的回报如下:

{
  "_embedded": {
    "sDDFServiceList": [
      {
        "content": {
          "serviceID": "FMS",
          "peerID": "FlightSim_0977",
          "peerName": "FlightSim",
          "translator": "SDDFTranslator:AdvTranslator",
          "transporter": "SDDFTransporter:CMSVTransporter#trumpet.mitre.org@41375",
          "contentIterator": [
.etc

我的问题是 - 有没有办法更改sDDFServiceList的名称。我尝试了各种注释,似乎没有任何改变(除了更改类名本身)

Class Def

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

我已经查看了几乎所有可以找到的堆栈帖子并尝试了大量不同的东西 - 但是我能想到的就是更改我的班级名称 - 这仍然会得到{{1附加到它的末尾。

为什么我不理解为什么单个List获得Resource字段但content对象似乎同时获得resources和{{1} } fields

1 个答案:

答案 0 :(得分:1)

您是否尝试在类本身上添加关系注释?像:

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  
<div id="mainCreate">
  v_total = {{ v_total}}
  <br>
    <span v-for="date in test">
      {{ date.title }}
      <input type="checkbox"
             name="featuresBox"
             v-on:change="date.price = (date.price ? 0 : 1)"
             v-bind:checked="date.price == 0"> date.price: {{ date.price }}
      <br>
    </span>

    <button onClick="f_test()">create object</button>
    <button onClick="changeV()">change app.v_total</button>
</div>