I have created RESTful service (POST) using Spring-MVC and I can access it successfully using POSTMAN. Now, I am trying to put validation to input json and I want to use Hibernate validator using its XML configuration. But validation is not triggering.
This is my service signature looks like:
@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();
}
Following is my 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>
I am referring to this document. And have created validation 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>
constraints-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>
I have placed both constraints-test.xml and validation.xml in 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;
}
}
Now when I trigger the service with following json
{
"manufacturer":"TOYOTA"
}
I get {"TOYOTA"}
when I send invalid json
{ "manufacturer":null }
I get {null}
Can someone please tell me why the hibernate validation is not getting triggered?
Thanks in advance
I have placed both constraints-test.xml and validation.xml in src/main/resources.
validation.xml
needs to live in META-INF
, i.e. it should be src/main/resources/META-INF
.
Is there btw. a reason for still using HV 4.2? It's really old by now, I recommend you upgrade to 5.2.2 or 4.3.2 (in case you need to stay on Bean Validation 1.0) if possible.