1.背景
在我们日常后台的开发中,涉及到很多的字段验证。
public void test(@RequestBody LoginRequest request) { if (request.getUsername() != null && request.getUsername().length() > 0) { 。。。 } }
如果request有很多参数,或者是一个表单提交,有很多字段,那我们不是要写很多这样的if判断,可是我们看这样的验证大部分都是相同的,唯独就是字段不一样,那有没有什么便捷的方法呢? 有,就是Hibernate Validateor验证框架,他可以使我们直接使用注解的方式,完成字段验证,加上注解后,剩下的我们只关注自己的逻辑了
2.添加依赖
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.0.2.Final</version> </dependency>
3.常用注解
4.当错误的时候,异常提示信息
javax.validation.constraints.AssertFalse.message = must be false javax.validation.constraints.AssertTrue.message = must be true javax.validation.constraints.DecimalMax.message = must be less than ${inclusive == true ? 'or equal to ' : ''}{value} javax.validation.constraints.DecimalMin.message = must be greater than ${inclusive == true ? 'or equal to ' : ''}{value} javax.validation.constraints.Digits.message = numeric value out of bounds (<{integer} digits>.<{fraction} digits> expected) javax.validation.constraints.Future.message = must be in the future javax.validation.constraints.Max.message = must be less than or equal to {value} javax.validation.constraints.Min.message = must be greater than or equal to {value} javax.validation.constraints.NotNull.message = may not be null javax.validation.constraints.Null.message = must be null javax.validation.constraints.Past.message = must be in the past javax.validation.constraints.Pattern.message = must match "{regexp}" javax.validation.constraints.Size.message = size must be between {min} and {max} org.hibernate.validator.constraints.CreditCardNumber.message = invalid credit card number org.hibernate.validator.constraints.Email.message = not a well-formed email address org.hibernate.validator.constraints.Length.message = length must be between {min} and {max} org.hibernate.validator.constraints.NotBlank.message = may not be empty org.hibernate.validator.constraints.NotEmpty.message = may not be empty org.hibernate.validator.constraints.Range.message = must be between {min} and {max} org.hibernate.validator.constraints.SafeHtml.message = may have unsafe html content org.hibernate.validator.constraints.ScriptAssert.message = script expression "{script}" didn't evaluate to true org.hibernate.validator.constraints.URL.message = must be a valid URL org.hibernate.validator.constraints.br.CNPJ.message = invalid Brazilian corporate taxpayer registry number (CNPJ) org.hibernate.validator.constraints.br.CPF.message = invalid Brazilian individual taxpayer registry number (CPF) org.hibernate.validator.constraints.br.TituloEleitor.message = invalid Brazilian Voter ID card number
5.在Controller里面验证使用
@RequestMapping("/register")
public CommonResp register(@Valid @RequestBody RegisterBean register, BindingResult bingingresult) {
// 如果验证不通过,错误信息会在BindingResult这个对象中
if (bingingresult.hasErrors()) {
return CommonResp(ErrorCode.xxx, bingingresult.getErrors().get(0).getDefaultMessage());
}
}
6.也可以自定义错误提示信息
public class UserBean { @NotEmpty(message="姓名不能为空") private String name; @Range(min=20,max=120,message="年龄在20到120岁之间") private int age; ...getter/setter... }