映射后的验证未调用ConstraintValidator的isValid函数。

huangapple 未分类评论43阅读模式
英文:

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(&quot;/management/upload/&quot;)
fun uploadMultipartFile(@FileNotEmpty @RequestParam(&quot;file&quot;) file: MultipartFile,
                        @Valid @ModelAttribute(&quot;form&quot;) model: FileUploadForm): ModelAndView {
    print(&quot;POST::RECEIVED&quot;)
    return ModelAndView(&quot;management/upload&quot;, &quot;form&quot;, 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 = &quot;{javax.validation.constraints.NotBlank.message}&quot;,
        val groups: Array&lt;KClass&lt;out Any&gt;&gt; = [],
        val payload: Array&lt;KClass&lt;out Any&gt;&gt; = []
)

Finally, the validator:

class FileNotEmptyValidatorForMultipartFile : ConstraintValidator&lt;FileNotEmpty, MultipartFile&gt; {

    override fun isValid(value: MultipartFile?, context: ConstraintValidatorContext?): Boolean {
        if (value == null) {
            return false
        }
        return !value.isEmpty || value.name.isNotEmpty()
    }
}

EDIT:

@RequestParam(&quot;file&quot;) 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.

huangapple
  • 本文由 发表于 2020年4月9日 19:49:05
  • 转载请务必保留本文链接:https://java.coder-hub.com/61120493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定