小编nyk*_*kon的帖子

为什么构造函数注入中@SpringBootTest需要@Autowired

一个更普遍的问题。如果在常规 Spring 托管类中使用构造函数注入,则类将自动装配,而不需要 @Autowired 注释,即:

\n
@Service\nclass MailService(\n  private val projectService: ProjectService,\n  private val mailer: Mailer\n) { ...\xc2\xa0}\n
Run Code Online (Sandbox Code Playgroud)\n

在 @SpringBootTest 类中遵循相同的构造函数注入原则,您需要将 @Autowired 注解设置为构造函数参数,否则将无法注入该类,即:

\n
@SpringBootTest\ninternal class MailerTest(\n  @Autowired private val mailer: Mailer\n) { ... }\n
Run Code Online (Sandbox Code Playgroud)\n

为什么会出现这种差异呢?

\n

testing junit spring mockito kotlin

7
推荐指数
1
解决办法
2249
查看次数

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

我有这个嵌套类

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 中数据的消耗和更新:(

rest spring kotlin

6
推荐指数
1
解决办法
1270
查看次数

标签 统计

kotlin ×2

spring ×2

junit ×1

mockito ×1

rest ×1

testing ×1