首页 > 解决方案 > 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 字段标记为非可选并且仍然违反约束?当我将它们标记为可选时,我必须使用 !! 将代码中的不可为空字段映射到我的实体时。

谢谢。

标签: javaspringkotlinbean-validation

解决方案


我相信你走错了路。

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 的行为


推荐阅读