在Spring验证和Hibernate验证器中遇到了一些麻烦。Spring Hibernate验证器无法正常工作,但是javax是
我使用的是Spring 4和Hibernate Validator 5,我的问题是javax注释行为正常,但看起来Hibernate注释没有被调用。
这里是我的控制器
@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);
}
,并在NOTNULL和Max(其现在注释掉)命令对象
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;
}
}
正在努力,但长度似乎被完全忽略。如果我使用Size,它会起作用,如果我使用任何Hibernate注释,它将被忽略。
我使用从春天
<bean id='validator' class='org.springframework.validation.beanvalidation.LocalValidatorFactoryBean'/>
的LocalValidatorFactoryBean为什么不适用Hibernate的验证?
编辑
继续尝试理解这里发生了什么。
我注意到NotEmpty验证是唯一有效的验证。我可以把它放在描述字段上,而不是发送它,我会得到错误。如果我在电子邮件,信用卡或网址上添加说明并发送错误的数据,我不会收到错误消息。
编辑
我已经创建了一些演示这种行为的一个小项目。它在我的bitbucket回购https://bitbucket.org/joeyoung/hibernate-validation-problems。
Form对象有4个公共属性,其中两个被注释掉,它们使用@Length和@Email验证器。
public class Form {
@Length(max=10)
public String name;
@Email
public String email;
// @Range(min=18)
// public Integer age;
// @CreditCardNumber
// public String creditCard;
}
如果我发布这个json;
{
"name":"adafasdfasfdsafd",
"email":"adf",
"age":1,
"creditCard":"123456"
}
到该控制器
@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;
}
}
返回任何错误。
如果我取消@Range字段的注释并重新发布,我会收到@Email和@Range错误,而不是@Length。
我假设我做错了什么,任何指导将不胜感激。
服务器明智的,我使用WebSphere 8.5的自由和我的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>
这是非常奇怪的..你能提供一个可以重现它的工作示例吗? – 2014-10-29 23:17:03
@SlavaSemushin,我做了一些编辑并链接到一个给我同样问题的项目。任何意见非常感谢。 – 2014-10-30 16:38:06