一个更普遍的问题。如果在常规 Spring 托管类中使用构造函数注入,则类将自动装配,而不需要 @Autowired 注释,即:
\n@Service\nclass MailService(\n private val projectService: ProjectService,\n private val mailer: Mailer\n) { ...\xc2\xa0}\nRun Code Online (Sandbox Code Playgroud)\n在 @SpringBootTest 类中遵循相同的构造函数注入原则,您需要将 @Autowired 注解设置为构造函数参数,否则将无法注入该类,即:
\n@SpringBootTest\ninternal class MailerTest(\n @Autowired private val mailer: Mailer\n) { ... }\nRun Code Online (Sandbox Code Playgroud)\n为什么会出现这种差异呢?
\n我有这个嵌套类
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 中数据的消耗和更新:(