Hi all,
I was trying to integrate Hibernate validation on a Maven Spring MVC project, with annotation system it works flawlessly but if I try to put rules on a separate xml using validation.xml on classpath project it stops to work.
The only feedback it gives to me in logs is:
Code:
org.hibernate.validator.internal.xml.ValidationXmlParser.unmarshal HV000007: META-INF/validation.xml found. Parsing XML based configuration.
The project specs are:
Netbeans 8.1
JDK 1.8.0_65
SpringMVC 4.2.4
Tomcat 8.0.27.0
Hibernate Validator 5.2.2.Final
here my validation.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<validation-config
xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration">
<constraint-mapping>META-INF/vincoliModel.xml</constraint-mapping>
<property name="hibernate.validator.fail_fast">false</property>
</validation-config>
vincoliModel.xml
Code:
<constraint-mappings
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.1.xsd"
xmlns="http://jboss.org/xml/ns/javax/validation/mapping" version="1.1">
<default-package>com.studiosviluppo.springmvc</default-package>
<bean class="model.User" ignore-annotations="true">
<field name="name">
<constraint annotation="javax.validation.constraints.Size">
<element name="min">1</element>
<element name="max">4000</element>
</constraint>
</field>
<field name="surname">
<constraint annotation="javax.validation.constraints.Size">
<element name="min">1</element>
<element name="max">4000</element>
<message>La lunghezza deve essere compresa fra 1 e 4000 caratteri</message>
</constraint>
</field>
<field name="birthDate">
<constraint annotation="org.springframework.format.annotation.DateTimeFormat">
<element name="pattern">dd/MM/yyyy</element>
</constraint>
</field>
</bean>
</constraint-mappings>
User.java (the model to validate)
Code:
package com.studiosviluppo.springmvc.model;
import java.util.Date;
import javax.validation.constraints.Size;
public class User {
private Long id;
private String name;
private String surname;
private Date birthDate;
private Long id_job;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getId_job() {
return id_job;
}
public void setId_job(Long id_job) {
this.id_job = id_job;
}
}
MVC Controller
Code:
package com.studiosviluppo.springmvc.controller;
import com.studiosviluppo.springmvc.model.User;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/utenteCon")
public class userControlloer {
@RequestMapping(method = RequestMethod.GET)
public String vediFormRegtistrazione(Map<String, Object> model) {
User utente = new User();
model.put("utente", utente);
return "registraUtente";
}
@RequestMapping(method = RequestMethod.POST)
public String actionFormRegistrazione(@ModelAttribute("utente") @Valid User utente, BindingResult resultUtente,
Map<String, Object> model
) {
// for testing purpose:
System.out.println("name: " + utente.getName());
System.out.println("surname: " + utente.getSurname());
System.out.println("birth date: " + utente.getBirthDate());
if (resultUtente.hasErrors()) {
return "registraUtente";
} else {
return "registrazioneUtenteOk";
}
}
}
Trying to solve it I noticed that
with validation.xml, annotations such as
Code:
@Size(min = 2, max = 4000)
private String name;
not work even if I set
ignore-annotations="false"and putting an inexistent file on
Code:
<constraint-mapping>META-INF/vincoliModelsadasdasd.xml</constraint-mapping>
not gives to me any
file not found error or something else on logs.
Thanks