使用hibernate验证器XML配置和Spring进行自动验证

时间:2015-12-15 07:22:16

标签: spring spring-mvc hibernate-validator

我使用Spring-MVC创建了RESTful服务(POST),我可以使用POSTMAN成功访问它。 现在,我正在尝试将验证放入输入json,我想使用其配置使用Hibernate验证器。但验证并未触发。

这是我的服务签名如下:

@RequestMapping(value = "/checkmycar", method = RequestMethod.POST, consumes = "application/json")
public String check(@Valid @RequestBody Car car, BindingResult result) {

    if (result.hasErrors()) {
        return "ERROR";
    }

    else
        return car.getManufacturer();
}

以下是我的POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test.hibernate-validator</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.10</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>

</dependencies>

我指的是this文件。并创建了验证xml

<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
                 xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
<default-package>org.hibernate.validator.quickstart</default-package>
<bean class="Car" ignore-annotations="true">
    <field name="manufacturer">
        <constraint annotation="javax.validation.constraints.NotNull"/>
    </field>
</bean>

约束-的test.xml

<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
                 xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
<default-package>org.hibernate.validator.quickstart</default-package>
<bean class="Car" ignore-annotations="true">
    <field name="manufacturer">
        <constraint annotation="javax.validation.constraints.NotNull"/>
    </field>
</bean>

我已将constraint-test.xml和validation.xml放在src / main / resources中。

Car.java

public class Car {

private String manufacturer;

public Car() {
}

public Car(String manufacturer) {
    this.manufacturer = manufacturer;
}
public String getManufacturer() {
    return manufacturer;
}

public void setManufacturer(String manufacturer) {
    this.manufacturer = manufacturer;
}
}

现在,当我使用以下json

触发服务时
{
"manufacturer":"TOYOTA"
}

我得到{“TOYOTA”}

当我发送无效的json

{     “制造商”:空     }

我得{null}

有人可以告诉我为什么没有触发hibernate验证吗?

提前致谢

1 个答案:

答案 0 :(得分:3)

  

我已将constraint-test.xml和validation.xml放在src / main / resources中。

validation.xml需要住在META-INF,即它应该是src/main/resources/META-INF

有顺便说一下吗?仍然使用HV 4.2的原因?它现在已经很老了,如果可能的话,我建议你升级到5.2.2或4.3.2(如果你需要继续使用Bean Validation 1.0)。