简体   繁体   English

将 Java 验证器用于带有 @Value 注释的 SpringBoot 属性

[英]Using Java validator for SpringBoot properties with @Value annotation

Does anybody know if it's possible to use custom ConstraintValidator for SpringBoot properties with @Value annotation?有谁知道是否可以将自定义 ConstraintValidator 用于带有@Value注释的 SpringBoot 属性?

Something like this:像这样:

@Component
@Validated
public class TestClass {
    
    @Value("${test.property}")
    @ValidValue(className=ValidValueValidator.class)
    private String testProperty;
    
}

what's your mean how to make @Validated Take effect?你的意思是如何让@Validated生效?

  • u can register a BeanValidationPostProcessor bean你可以注册一个BeanValidationPostProcessor bean

or if your want use custom ConstraintValidator或者如果你想使用自定义 ConstraintValidator

  • see Majutharan Majutharan comment见 Majutharan Majutharan 评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM