java - Kotlin Spring bean 验证可空性
问题描述
在我用 Kotlin 构建的 Spring 应用程序中,我想对看起来像这样的数据类使用 bean 验证。
data class CustomerDto(
@field: NotBlank
val firstName: String,
@field: NotBlank
val lastName: String)
在向客户端点发送一个空名字的帖子时,我想获得约束验证,但由于字段不允许空值,我没有得到验证,而是得到以下错误。
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Instantiation of [simple type, class pkg.CustomerDto] value failed for JSON property firstName due to missing (therefore NULL) value for creator parameter firstName which is a non-nullable type; nested exception is com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class pkg.CustomerDto] value failed for JSON property firstName due to missing (therefore NULL) value for creator parameter firstName which is a non-nullable type\n at [Source: (PushbackInputStream); line: 19, column: 1] (through reference chain: pkg.CustomerDto[\"firstName\"])",
"path": "/shop/5/customer"
是否有任何其他选项可以将 dto 字段标记为非可选并且仍然违反约束?当我将它们标记为可选时,我必须使用 !! 将代码中的不可为空字段映射到我的实体时。
谢谢。
解决方案
我相信你走错了路。
Kotlin 的空安全运算符的确切目的是强制您在代码中明确表达可空性行为,以大幅减少 NPE,或者至少确保您自己故意造成它们:)。在您(或任何类似 MVC 的访问模式)的情况下,您将面临以下情况
- 作为 DTO 有效负载的一部分,字段可能为空
- 如果字段为空,框架验证应该取消 DTO 的资格。
- 如果框架验证成功,则隐式假定 DTO 字段不为 null。
尽管从逻辑流的角度来看它是有意义的,但它实际上是可能导致 NPE 的违规行为,因为模型/合同中的任何内容都不能保证这些字段不会为空
不过,在 java 中,您只是使用 getter 做出了最后的假设(无论如何,您一直在使用 getter,它是 java,对吧?)。
好吧 - 在 kotlin 中没有什么不同,如果这是你需要的:
data class CustomerDto(@field:NotNull
@JsonProperty("firstName") private val _firstName: String? = null,
@field:NotNull
@JsonProperty("lastName") private val _lastName: String? = null) {
val firstName get() = _firstName!!
val lastName get() = _lastName!!
}
(此示例假设您jackson
用于 JSON 反序列化)
虽然仍然使用运算符手动强制不可为空!!
(这是您想要避免的),但您现在正在从代码库的其余部分中抽象出该方面,获得类似 java-getter 的行为
推荐阅读
- python - mypy Error TypeVar with Value Restriction and Union of Unions / Optional Cannot Pass generic container type
- symfony - Symfony 4 抛出 UndefinedMethodException 异常
- sapui5 - SAPUI5 为选择元素添加描述/工具提示
- singleton - 单例中的 Monolog 实现
- vba - 我怎样才能宏Word字体
- c# - 如何在注销按钮上使用带有提交类型的 Font Awesome
- vbscript - asp 应用程序按钮无法正确执行并且不执行服务器调用
- amazon-web-services - 访问被拒绝在 s3 存储桶上公开图像
- typescript - “V”型不能分配给“V”型。[2719]
- asp.net-mvc - 使用 MVC 在加载时自动打开一个弹出窗口