Spring Rest中的自定义JsonConfiguration
我正在从SpringHTTP响应返回一个Spring Rest中的自定义JsonConfiguration,json,spring-mvc,kotlin,Json,Spring Mvc,Kotlin,我正在从SpringHTTP响应返回一个@Serializable对象。它被正确地序列化为JSON,正如预期的那样,代码大致如下所示: @Serializable @SerialName("example") data class Example (...): BaseExample(...) @GetMapping("/path") fun getExample(): Example = Example(...) 该对象是类层次结构的成员,因此我希望响应在字段type中包含序列名,因此我希
@Serializable
对象。它被正确地序列化为JSON,正如预期的那样,代码大致如下所示:
@Serializable
@SerialName("example")
data class Example (...): BaseExample(...)
@GetMapping("/path")
fun getExample(): Example = Example(...)
该对象是类层次结构的成员,因此我希望响应在字段
type
中包含序列名,因此我希望使用classDiscriminator=“type”
提供自定义JsonConfiguration
。这是如何做到的?到目前为止,我能够通过手动序列化解决它:
@GetMapping("/path", produces=[MediaType.APPLICATION_JSON_VALUE])
fun getExample(): String {
val json = Json(JsonConfiguration(classDiscriminator = "type"))
val result = ...
return json.stringify(Example.serializer(), result)
}
因此,除非有更优雅的解决方案,否则我们将坚持这种方式