3

如何使用 Hibernate 验证传入的参数?

在 XML 中

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor">
    <property name="validator" ref="validator"/>
</bean>

在 Java 中

 @Validated
 public class UserService

 @Override
 @NotNull
 public User registerUser(@NotEmpty String name, String username, String password, boolean google, boolean facebook)

这种方法不起作用我用错误参数调用方法并且验证不起作用。

4

4 回答 4

5

问题说明

当使用带有 Spring-MVC 4 的 Hibernate Validator (HV) 4.2 时,例如带有依赖项:

dependencies {
    providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
    compile 'org.springframework:spring-webmvc:4.0.3.RELEASE'
    compile 'org.hibernate:hibernate-validator:4.2.0.Final'
    runtime 'javax.servlet:jstl:1.1.2'
}

@Validated可以通过在配置文件中声明以下 bean来告诉 Spring 自动使用 HV 来验证带有注释的类的方法调用(如在 asker 的示例中),

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/>

当目标的方法被调用并且有一些违反其约束的东西时,将MethodConstraintViolationException被抛出。例如,如果一个人碰巧调用UserService#registerUser(...),就会引发类似这样的事情,将一个空字符串传递给带有注释的地方@NotEmpty

... The following constraint violations occurred: [MethodConstraintViolationImpl
[method=public abstract void foo.UserService.registerUser(java.lang.String),
parameterIndex=0, parameterName=arg0, kind=PARAMETER, message=may not be empty, 
messageTemplate={org.hibernate.validator.constraints.NotEmpty.message}, ...

但问题是,如果您将依赖项从上面更改为

dependecies {
    providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
    compile 'org.springframework:spring-webmvc:4.0.3.RELEASE'
    compile 'org.hibernate:hibernate-validator:5.1.0.Final' //**note the change here**
    runtime 'javax.servlet:jstl:1.1.2'
}

那么你会得到一个错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'org.springframework.validation.beanvalidation.MethodValidationPostProcessor#0'
defined in class path resource [spring/config/web/main-config.xml]: Invocation of init
method failed; nested exception is javax.validation.ValidationException: Unable to
instantiate Configuration.

解决方案

向类路径添加 2 个额外的依赖项,如下所示,

dependecies {
    providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
    providedCompile 'javax.el:el-api:2.2'
    providedCompile 'org.glassfish.web:el-impl:2.2'
    compile 'org.springframework:spring-webmvc:4.0.3.RELEASE'
    compile 'org.hibernate:hibernate-validator:5.1.0.Final' //**note the change here**
    runtime 'javax.servlet:jstl:1.1.2'
}

验证将恢复活力。但这一次,MethodConstraintViolationException它没有提出ConstranitViolationException任何违反的细节,而不是 。

我不确定 Spring 如何在内部管理这个问题,以及你是否可以从这个异常中展开违规细节。HV 5 不再支持MethodConstraintViolationException及其亲属,MethodValidator例如MethodConstraintViolation. 这与Bean Validation 1.1 规范有关,它在 BV 1.0 的方法级验证方面有一些变化。我建议您自己使用 aspect 和 with ExecutableValidator(而不是 old MethodValidator)创建验证服务,如HV's Reference guide中所述。

请注意,如果您尝试像这样获取验证器,

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
ExecutableValidator executableValidator = factory.getValidator().forExecutables();

你可能会得到ValidationException: Unable to instantiate configuration.我可以通过这样做来解决这个问题

ValidatorFactory factory = Validation.byProvider(HibernateValidator.class).configure().buildValidatorFactory();
ExecutableValidator executableValidator = factory.getValidator().forExecutables();
于 2014-04-30T09:20:06.990 回答
4

除了 spring 和其他项目依赖项之外,我在 maven pom 文件中还包括以下内容。

<properties>
    <hibernate.version>4.3.1.Final</hibernate.version>
    <hibernate.validator.version>5.1.0.Final</hibernate.validator.version>
    <javax.el.version>3.0.0</javax.el.version>
    <asm.version>3.3.1</asm.version>
</properties>
<dependencies>
    <!-- Hibernate et al-->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${hibernate.validator.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>${javax.el.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>javax.el</artifactId>
        <version>${javax.el.version}</version>
    </dependency>
    <!-- asm allows hibernate validator to retrieve method parameter names when reporting method validation
        without this you just get arg0 instead of argument name -->
    <dependency>
        <groupId>asm</groupId>
        <artifactId>asm</artifactId>
        <version>${asm.version}</version>
    </dependency>
</dependencies>

在我为 spring 设置的 java 配置中

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("validation");
    messageSource.setCacheSeconds(1);

    return messageSource;
}


@Bean
LocalValidatorFactoryBean getValidator() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.setValidationMessageSource(messageSource());
    validator.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());
    return validator;

}

@Bean
@Autowired
MethodValidationPostProcessor getValidationPostProcessor(LocalValidatorFactoryBean validator) {
    MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
    processor.setValidator(validator);
    return processor;
}

LocalVariableTableParameterNameDiscoverer 与 ASM 一起使用,在 ConstraintViolationException 中提供正确的参数名称,当验证失败时将抛出该名称。

于 2015-01-30T16:21:19.497 回答
0

对于当前版本,您不必添加“asinkxcoswt”在https://stackoverflow.com/a/23384229/6028739中编写的所有 bean 。

在您的 pom.xml 中添加以下内容:

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-validator</artifactId>
   <version>5.3.4.Final</version>
</dependency>
<dependency>
   <groupId>org.glassfish.web</groupId>
   <artifactId>javax.el</artifactId>
   <version>2.2.4</version>
</dependency>

将此行添加到您的应用程序上下文中:

<bean id="validationFactory" class="javax.validation.Validation"  
      factory-method="buildDefaultValidatorFactory" />  

所有使用 @Validated 注释的实体现在都经过验证,并抛出javax.validation.ConstraintViolationException带有详细信息的约束违规列表,例如

javax.validation.ConstraintViolationException: Validation failed for classes [com.example.Class] during update time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='must be greater than or equal to 0', propertyPath=amount, rootBeanClass=class com.example.Class, messageTemplate='{javax.validation.constraints.Min.message}'}
]
于 2017-01-29T10:38:00.007 回答
0

显然 Spring 建议不要使用 ExecutableValidator 而是 MethodValidationInterceptor,请参阅 javadoc:http ://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.html

于 2015-09-15T11:09:08.190 回答