Kotlin是否需要NotNull? [英] Is NotNull needed on Kotlin?

查看:259
本文介绍了Kotlin是否需要NotNull?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堂课

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆