使用Spring进行的JSR-303 Bean验证无法启动 [英] JSR-303 bean validation with Spring does not kick in

查看:92
本文介绍了使用Spring进行的JSR-303 Bean验证无法启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照docs( https://github.com/abhijitsarkar/java/tree/master/spring-jsr-303 ),以及单元测试失败.如果您决定看一眼,只需克隆它并运行 根目录中的gradlew clean test. 我正在使用Spring框架4.0.2.RELEASE和Hibernate验证程序5.0.3.Final.

I've configured a JSR-303 custom validator following what's given in the docs (http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/validation.html), complete with LocalValidatorFactoryBean and Hibernate validator on the classpath. However, my validator just refuses to kick in. I've put up a dirt simple test project here (https://github.com/abhijitsarkar/java/tree/master/spring-jsr-303), along with a failing unit test. Should you decide to take a look, just clone it and run gradlew clean test from the root directory. I'm using Spring framework 4.0.2.RELEASE and Hibernate validator 5.0.3.Final.

正在验证的方法:

public Coffee serve(@ValidOrder(Coffee.Blend.class) final String blend) {

ValidOrder批注:

ValidOrder annotation:

@Documented
@Constraint(validatedBy = {OrderValidator.class})
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,
        ElementType.FIELD,
        ElementType.ANNOTATION_TYPE,
        ElementType.CONSTRUCTOR,
        ElementType.PARAMETER})
@NotNull
public @interface ValidOrder {

OrderValidator验证器:

OrderValidator validator:

public class OrderValidator implements ConstraintValidator<ValidOrder, String> {

Spring配置:

@Configuration
@ComponentScan(basePackages = "name.abhijitsarkar.coffeehouse")
@EnableAspectJAutoProxy
public abstract class AppConfig {

    @Bean
    public LocalValidatorFactoryBean validator() {
        return new LocalValidatorFactoryBean();
    }
}

依赖项:

dependencies {
    compile(
            [group: 'javax.inject', name: 'javax.inject', version: injectApiVersion],
            [group: 'javax.validation', name: 'validation-api', version: beanValidationApiVersion],
            [group: 'javax.annotation', name: 'javax.annotation-api', version: annotationApiVersion],
            [group: 'org.springframework', name: 'spring-beans', version: springVersion],
            [group: 'org.springframework', name: 'spring-context', version: springVersion],
            [group: 'org.springframework', name: 'spring-aop', version: springVersion],
            [group: 'org.aspectj', name: 'aspectjrt', version: aspectjVersion]
    )
    runtime(
            [group: 'org.hibernate', name: 'hibernate-validator', version: hibernateValidatorVersion],
            [group: 'javax.el', name: 'javax.el-api', version: elVersion],
            [group: 'org.glassfish.web', name: 'javax.el', version: glassfishElVersion],
            [group: 'org.aspectj', name: 'aspectjweaver', version: aspectjVersion]
    )

推荐答案

  1. 除了LocalValidatorFactoryBean,还需要配置MethodValidationPostProcessor.
  2. 要验证的类上必须带有@Validated批注,否则不会在方法中搜索内联约束批注.

  1. A MethodValidationPostProcessor needs to be configured in addition to the LocalValidatorFactoryBean.
  2. The class to be validated must have a @Validated annotation on it else methods are NOT searched for inline constraint annotations.

@Configuration
@ComponentScan(basePackageClasses = {SpringPackageComponentScanMarker.class})
@EnableAspectJAutoProxy
public abstract class AppConfig {

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
    final MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
    methodValidationPostProcessor.setValidator(validator());

    return methodValidationPostProcessor;
}

@Bean
public LocalValidatorFactoryBean validator() {
    final LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();

    return localValidatorFactoryBean;
  }
}

...

@Service
@Validated
public class SpringBarista extends Barista {

参考手册中有关与JSR-303集成的部分方便地忽略了这2个关键点,没有BV就无法发挥这两个关键点.这仅导致我进行了6个小时的调试和撕裂,而我在文档中所做的所有工作却无济于事. BV根本不会发挥作用.我最终不得不通过Spring源代码进行调试才能理解这一点.必须有一种更简单的方法,我不能成为唯一遇到此问题的人.为他们创建了一个JIRA SPR-11473 来更新文档.

The part of the reference manual that talks about integration with JSR-303 conveniently omits these 2 crucial points without which BV does not kick in. This just caused me 6 hours of debugging and hair tearing where I did everything the doc said but BV would simply not kick in. I finally had to debug through the Spring source code to understand this. There got to be an easier way and I can't be the only one who had this problem. Created a JIRA SPR-11473 for them to update the doc.

这篇关于使用Spring进行的JSR-303 Bean验证无法启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆