英文:
Validation during post mapping doesn't call ConstraintValidator's isValid function
问题
我已经在其中一个控制器中实现了POST映射:
@PostMapping("/management/upload/")
fun uploadMultipartFile(@FileNotEmpty @RequestParam("file") file: MultipartFile,
@Valid @ModelAttribute("form") model: FileUploadForm): ModelAndView {
print("POST::RECEIVED")
return ModelAndView("management/upload", "form", FileUploadForm())
}
不幸的是,请求在不调用isValid()
函数的情况下被接收,我已经在下面的验证器中实现了它。我尝试了许多(@FileNotEmpty, @RequestParam(...), @Valid
)的排列组合,但都不起作用。我不知道为什么这个验证没有被调用,尽管它被这个注解标记。
这是表单类:
data class FileUploadForm(
@FileNotEmpty
val file: MultipartFile? = null
)
验证注解:
@Constraint(validatedBy = [FileNotEmptyValidatorForMultipartFile::class])
@Target(AnnotationTarget.FIELD,
AnnotationTarget.FUNCTION,
AnnotationTarget.FILE,
AnnotationTarget.PROPERTY,
AnnotationTarget.ANNOTATION_CLASS,
AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.VALUE_PARAMETER)
@Retention(AnnotationRetention.RUNTIME)
annotation class FileNotEmpty (
val message: String = "{javax.validation.constraints.NotBlank.message}",
val groups: Array<KClass<out Any>> = [],
val payload: Array<KClass<out Any>> = []
)
最后,验证器:
class FileNotEmptyValidatorForMultipartFile : ConstraintValidator<FileNotEmpty, MultipartFile> {
override fun isValid(value: MultipartFile?, context: ConstraintValidatorContext?): Boolean {
if (value == null) {
return false
}
return !value.isEmpty || value.name.isNotEmpty()
}
}
编辑:
@RequestParam("file")
提供了正确的值,但与此同时,我的表单模型中的file
属性为null。由于我使用thymeleaf创建表单和验证注解,我需要在我的模型对象中有正确的file
值。
英文:
I have implemented post mapping in one of controller's:
@PostMapping("/management/upload/")
fun uploadMultipartFile(@FileNotEmpty @RequestParam("file") file: MultipartFile,
@Valid @ModelAttribute("form") model: FileUploadForm): ModelAndView {
print("POST::RECEIVED")
return ModelAndView("management/upload", "form", FileUploadForm())
}
Unfortunately, the request is received without calling isValid()
function, which I have implemented in the validator below. I've tried many permutations of (@FileNotEmpty, @RequestParam(...), @Valid
), but neither worked. I have no idea why this validation isn't being called, while it's marked by this annotation.
Here is the form class:
data class FileUploadForm(
@FileNotEmpty
val file: MultipartFile? = null
)
Validation annotation:
@Constraint(validatedBy = [FileNotEmptyValidatorForMultipartFile::class])
@Target(AnnotationTarget.FIELD,
AnnotationTarget.FUNCTION,
AnnotationTarget.FILE,
AnnotationTarget.PROPERTY,
AnnotationTarget.ANNOTATION_CLASS,
AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.VALUE_PARAMETER)
@Retention(AnnotationRetention.RUNTIME)
annotation class FileNotEmpty (
val message: String = "{javax.validation.constraints.NotBlank.message}",
val groups: Array<KClass<out Any>> = [],
val payload: Array<KClass<out Any>> = []
)
Finally, the validator:
class FileNotEmptyValidatorForMultipartFile : ConstraintValidator<FileNotEmpty, MultipartFile> {
override fun isValid(value: MultipartFile?, context: ConstraintValidatorContext?): Boolean {
if (value == null) {
return false
}
return !value.isEmpty || value.name.isNotEmpty()
}
}
EDIT:
@RequestParam("file")
provides me proper value, but at the same time file
property in my form model is null. Since I use thymeleaf to create forms and annotations for validation, I need to have proper file
value inside my model object.
专注分享java语言的经验与见解,让所有开发者获益!
评论