Spring boot 关于Hateoas+;SpringBoot&x2B;Kotlin(结构化响应)
据我所知,Spring Hateoas支持两种(或至少两种)格式:Spring boot 关于Hateoas+;SpringBoot&x2B;Kotlin(结构化响应),spring-boot,jackson,spring-hateoas,Spring Boot,Jackson,Spring Hateoas,据我所知,Spring Hateoas支持两种(或至少两种)格式:collection+json&hal 我正在尝试使用HAL格式(我想),但我的一个元素无法正确渲染 函数1:(返回单个资源) 我喜欢这个,因为我们的根是内容,我们有HAL风格的\u链接 版本2-多次返回 我的问题是,当我开始返回一个集合时,事情变得有点古怪: @GetMapping("/test3/{array_index}") fun test3(@PathVariable array_index: Int): Respons
collection+json
&hal
我正在尝试使用HAL
格式(我想),但我的一个元素无法正确渲染
函数1:(返回单个资源)
我喜欢这个,因为我们的根是内容
,我们有HAL风格的\u链接
版本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
)
}
我的问题是-有没有办法更改sDDFServiceList
的名称。我尝试了各种注释,但似乎没有任何改变(除了更改类名本身)
类别定义
我已经浏览了几乎每一篇我能找到的堆栈文章,并尝试了很多不同的东西——但目前为止,我能想到的就是更改我的类名——这仍然会将列表添加到它的末尾
我不明白的原因是为什么一个资源
得到一个内容
字段,而资源
对象似乎同时嵌入了\u和列表
字段您尝试过在类本身上添加关系注释吗?比如:
import org.springframework.hateoas.core.Relation;
@Relation(collectionRelation = "services")
public class SDDFService extends ResourceSupport {
}
不幸的是,这个类来自一个外部库-我想我可以对它进行包装?但是我包装了-我的内部类-添加了这个注释,您已经解决了我的问题!:)谢谢
@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
@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"))
}
}
import org.springframework.hateoas.core.Relation;
@Relation(collectionRelation = "services")
public class SDDFService extends ResourceSupport {
}