未调用自定义 Spring 注释
Custom Spring annotation not called
在我的 Spring 引导项目中,我创建了一个带有验证器扩展 ConstraintValidator
的自定义注释,以验证 RequestBody 中的某些字段。注释对于非嵌套字段工作正常,但不会为嵌套字段调用验证器。
我的注释看起来像:
@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
@Constraint(validatedBy = [CustomValidator::class])
@Suppress("unused")
@MustBeDocumented
annotation class CustomValidation(
val message: String = "validation failed",
val groups: Array<KClass<*>> = [],
val payload: Array<KClass<out Payload>> = []
)
我的验证者class:
@Component
class CustomValidator : ConstraintValidator<CustomValidation, String> {
override fun isValid(field: String?, context: ConstraintValidatorContext?): Boolean {
if (field != "example") {
return false
}
return true
}
}
在这种情况下效果很好:
data class MyRequest(
// validator works perfectly here
@JsonProperty("example") @CustomValidation val example: String? = null,
@JsonProperty("locale") val locale: String? = null
)
但是当放在嵌套对象上时,不会调用验证器:
data class MyRequest(
@JsonProperty("nested") val nested: NestedClass? = null,
@JsonProperty("locale") val locale: String? = null
)
data class NestedClass(
// validator not called in that case
@JsonProperty("example") @CustomValidation val example: String? = null
)
MyRequest
class 在我的 RestController
中的用法:
@PostMapping("/endpoint")
fun doSomething(
@Valid @RequestBody myRequest: MyRequest,
@RequestHeader(value = "token") token: String
): ResponseEntity<MyResponse> = ResponseEntity.ok(myService.getData(myRequest))
关于如何解决该问题的任何想法?
我已经尝试在 nested
字段上添加 @Valid 注释,但它仍然不起作用
通常要在嵌套 class 中进行验证,您需要使用与 @Valid
相同的嵌套 class 类型来注释父 class 的字段。
此处,要对 NestedClass
class 进行验证,您需要将 @Valid
添加到 MyRequest
的 nested
字段 class。由于它是构造函数的一部分,因此应该使用 Annotation Use-site Targets 完成,如下所示:
data class MyRequest(
@field:Valid @JsonProperty("nested") val nested: NestedClass? = null,
@JsonProperty("locale") val locale: String? = null
)
您的 @CustomValidation
注释在不使用 use-site 目标的情况下工作的原因是因为它只有一个用 @Target(AnnotationTarget.FIELD)
定义的目标,而 @Valid
注释有多个可能的目标,即 @Target(value={METHOD,FIELD,CONSTRUCTOR,PARAMETER})
,因此您需要有 use-site 目标来告诉编译器正确的目标。
在我的 Spring 引导项目中,我创建了一个带有验证器扩展 ConstraintValidator
的自定义注释,以验证 RequestBody 中的某些字段。注释对于非嵌套字段工作正常,但不会为嵌套字段调用验证器。
我的注释看起来像:
@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
@Constraint(validatedBy = [CustomValidator::class])
@Suppress("unused")
@MustBeDocumented
annotation class CustomValidation(
val message: String = "validation failed",
val groups: Array<KClass<*>> = [],
val payload: Array<KClass<out Payload>> = []
)
我的验证者class:
@Component
class CustomValidator : ConstraintValidator<CustomValidation, String> {
override fun isValid(field: String?, context: ConstraintValidatorContext?): Boolean {
if (field != "example") {
return false
}
return true
}
}
在这种情况下效果很好:
data class MyRequest(
// validator works perfectly here
@JsonProperty("example") @CustomValidation val example: String? = null,
@JsonProperty("locale") val locale: String? = null
)
但是当放在嵌套对象上时,不会调用验证器:
data class MyRequest(
@JsonProperty("nested") val nested: NestedClass? = null,
@JsonProperty("locale") val locale: String? = null
)
data class NestedClass(
// validator not called in that case
@JsonProperty("example") @CustomValidation val example: String? = null
)
MyRequest
class 在我的 RestController
中的用法:
@PostMapping("/endpoint")
fun doSomething(
@Valid @RequestBody myRequest: MyRequest,
@RequestHeader(value = "token") token: String
): ResponseEntity<MyResponse> = ResponseEntity.ok(myService.getData(myRequest))
关于如何解决该问题的任何想法?
我已经尝试在 nested
字段上添加 @Valid 注释,但它仍然不起作用
通常要在嵌套 class 中进行验证,您需要使用与 @Valid
相同的嵌套 class 类型来注释父 class 的字段。
此处,要对 NestedClass
class 进行验证,您需要将 @Valid
添加到 MyRequest
的 nested
字段 class。由于它是构造函数的一部分,因此应该使用 Annotation Use-site Targets 完成,如下所示:
data class MyRequest(
@field:Valid @JsonProperty("nested") val nested: NestedClass? = null,
@JsonProperty("locale") val locale: String? = null
)
您的 @CustomValidation
注释在不使用 use-site 目标的情况下工作的原因是因为它只有一个用 @Target(AnnotationTarget.FIELD)
定义的目标,而 @Valid
注释有多个可能的目标,即 @Target(value={METHOD,FIELD,CONSTRUCTOR,PARAMETER})
,因此您需要有 use-site 目标来告诉编译器正确的目标。