introduced jar package:
< dependency >
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
< / dependency >
< dependency >
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.1.Final</version>
< / dependency >
Spring version 4.3
entity class:
public class Spitter {
//private Long id;
//@NotNull
@Length(min=5,message="5")
private String username;
//@NotNull
@Length(min=5,message="5")
private String password;
//@NotNull
@Length(min=2,message="5")
private String firstName;
//@NotNull
@Length(min=2,message="5")
private String lastName;
public Spitter() {
}
Controller:
RequestMapping (value = "/ register", method = POST)
public String processRegistration (@ Validated Spitter spitter, BindingResult errors) {
if(errors.hasErrors()){
return "registerForm";
}
System.out.println("has no errors");
spitterRepository.save(spitter);
return "redirect:/spitter/" + spitter.getUsername();
}
debug:
Why not report an error when it is clear that the length does not match