Spring Hibernate validator not working but javax
Some problems with Spring validation and hibernation validator.
I am using Spring 4 and Hibernate Validator 5, my problem is the javax annotations are working correctly, but it looks like Hibernate annotations are not being called.
Here is my controller
@RequestMapping(method= RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody @Valid CreatePlantCommand command, BindingResult bindingResult, HttpServletResponse response){
if (bindingResult.hasErrors()) {
// do something
}
// do work
// should set the location header on new requests
String location = ServletUriComponentsBuilder.fromCurrentRequest()
.pathSegment("{code}").buildAndExpand(code)
.toUriString();
response.setHeader("Location", location);
}
and command object
public class CreatePlantCommand {
@NotNull
private Integer code;
@Length(min = 1, max = 5)
//@Max(5)
private String description;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
NotNull and Max (his comment now) work, but Length seems to be completely ignored. If I use Size it works, if I use Hibernate annotation it is ignored.
I am using LocalValidatorFactoryBean from Spring
<bean id='validator' class='org.springframework.validation.beanvalidation.LocalValidatorFactoryBean'/>
Why is Hibernate validation not being applied?
EDIT
Continuing to try to understand what's going on here.
I noticed that the NotEmpty check is the only one that works. I can put it in the description field instead of submitting it and I get an error. If I put Email, CreditCard or Url on the description and send incorrect data to it, I won't get an error.
EDIT
I've created a small project demonstrating some of this behavior. This is on my bitbucket replica at https://bitbucket.org/joeyoung/hibernate-validation-problems .
The Form object has 4 public properties, two of which are commented out, which are used by the @Length and @Email validators.
public class Form {
@Length(max=10)
public String name;
@Email
public String email;
// @Range(min=18)
// public Integer age;
// @CreditCardNumber
// public String creditCard;
}
If I post this json,
{
"name":"adafasdfasfdsafd",
"email":"adf",
"age":1,
"creditCard":"123456"
}
To this controller
@RestController
@RequestMapping("/form")
public class FormController {
@RequestMapping(method = RequestMethod.POST, consumes = "application/json")
public List<FieldError> submitForm(@RequestBody @Valid Form form, BindingResult result) {
if(result.hasErrors()) {
return result.getFieldErrors();
}
return null;
}
}
No errors are returned.
If I uncomment the @Range field and post again, I get @Email and @Range errors, not @Length.
I guess I am doing something wrong and any guidance would be greatly appreciated.
Server wise, I am using WebSphere Liberty 8.5 and my server.xml
<?xml version="1.0" encoding="UTF-8"?>
<server description="Default Liberty Server">
<!-- Enable features -->
<featureManager>
<feature>jsp-2.2</feature>
<feature>jndi-1.0</feature>
<feature>jdbc-4.0</feature>
<feature>jpa-2.0</feature>
<feature>localConnector-1.0</feature>
<feature>beanValidation-1.0</feature>
</featureManager>
<!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
<httpEndpoint id="defaultHttpEndpoint" httpPort="9080" httpsPort="9443" />
<applicationMonitor updateTrigger="mbean" />
<application id="problems_war_exploded" location="C:\Users\jyoung\Code\hibernate-validation-problems\target\problems-0.0.1-SNAPSHOT" name="problems_war_exploded" type="war" context-root="/hibernate-validation-problems" />
</server>
source to share