Spring在JSON序列化中将属性名转换为小写

nyk*_*kon 6 rest spring kotlin

我有这个嵌套类

data class UI(
val xPosition: Int,
val yPosition: Int
)
Run Code Online (Sandbox Code Playgroud)

当对象通过 @RestController 序列化时,我在 HTTP 端点上收到所有小写字母(最后一行是有趣的一行):

{"id":"c8a7b735-b407-4dae-9a41-a470f7596895",
"isActive":false,
"title":"First Sample",
"description":{"shortDescription":"...","longDescription":"...","additionalNotes":"..."},
"endDate":"2021-01-07","priority":0,
"ui":{"xposition":0,"yposition":0}}]   <<<<<< see here
Run Code Online (Sandbox Code Playgroud)

我将数据复制到 MongoDB 文档中。那里正确地指出了

ui:
xPosition: 0
yPosition: 0
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?我无法让 Spring RestController 将第二个字母返回为大写。这会破坏附加 UI 中数据的消耗和更新:(

小智 2

由于Java限制,Json的前2个字母必须小写;我想唯一的解决方案是使用你的变量:

val xposition: Int,
val yposition: Int
Run Code Online (Sandbox Code Playgroud)