JSR 303 Bean Validation (Hiberna
2016-11-07 本文已影响0人
Tony_HQ
附上配置文件(突然换了gradle别见怪! 如果validation-api 和hibernate validtor 两者不一致,则会出现getDefaultParameters......
错误之类的)
def hibernateValidtorVersion = "5.1.3.Final"
def validtorApiVersion = "1.1.0.Final"
compile(
//validtor
"org.hibernate:hibernate-validator:$hibernateValidtorVersion",
"javax.validation:validation-api:$validtorApiVersion",
)
使用示例配置JSON映射
public class User {
@NotNull(message="name.not.empty")
private String name;
@Range(min=0,max=100,message="age.not.inrange")
private int age;
}
使用
@Override
public Map<String, Object> addUser(@Valid @RequestBody User user, BindingResult bindingResult) throws Exception {
Integer r = userService.addUser(user);
for(ObjectError error :bindingResult.getAllErrors()){
throw new Exception(
error.getDefaultMessage());
}
return CommonUtils.returnMesMap("200", "添加成功", "");
}