Kotlin是否需要NotNull? [英] Is NotNull needed on Kotlin?
问题描述
我有一堂课
class User(
var name: String
)
以及映射的帖子请求:
@PostMapping("/user")
fun test(@Valid @RequestBody user: User) {
//...
}
如果客户端将使用name: null
发送用户的JSON怎么办?它会被MVC验证程序拒绝还是会引发异常?我应该用@NotNull
注释name
吗?不幸的是,我无法检查,因为只能编写测试(无法创建User(null)
).
What if a client will send a JSON of a user with name: null
? Will it be rejected by the MVC Validator or an exception will be throwed? Should I annotate name
with @NotNull
? Unfortunately, I cannot check that because only can write tests (it is not available to create User(null)
).
推荐答案
如我所测试的,@NotNull
完全不影响MVC验证.在正常的控制台中,您只会收到WARN
消息:
As I tested, @NotNull
doesn't affect on the MVC validation at all. You will only receive a WARN
message in console which is normal:
无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException:无法读取文档:...的实例化.
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Instantiation of...
这篇关于Kotlin是否需要NotNull?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!