Kotlin Spring Bean 验证可空性
2022-09-03 02:49:12
在我用 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 字段标记为非可选,并且仍然遇到约束冲突?当我将它们标记为可选时,我必须使用!!在将代码中的不可为 null 字段映射到我的实体时。
谢谢。