一、简介
开发web项目有时候我们需要对controller层传过来的参数进行一些基本的校验,比如非空,非null,整数值的范围,字符串的个数,日期,邮箱等等。最常见的就是我们直接写代码校验,这样以后比较繁琐,而且不够灵活。
Bean Validation 1.0(JSR-303)是一个校验规范,在Spring Boot项目由于自带了Hibernate validator 5(http://hibernate.org/validator/)实现,所以我们可以非常方便的使用这个特性
二、关键使用
1、添加包
hibernate-validator
<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator --> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.2.Final</version> </dependency>
或者添加spring-boot-starter-validation
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> <version>1.4.0.RELEASE</version> </dependency>
或者添加spring-boot-starter-web
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2、注解说明JSR提供的校验注解:
@Null 被注释的元素必须为 null
@NotNull 被注释的元素必须不为 null
@AssertTrue 被注释的元素必须为 true
@AssertFalse 被注释的元素必须为 false
@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max=, min=) 被注释的元素的大小必须在指定的范围内
@Digits (integer, fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past 被注释的元素必须是一个过去的日期
@Future 被注释的元素必须是一个将来的日期
@Pattern(regex=,flag=) 被注释的元素必须符合指定的正则表达式
Hibernate Validator提供的校验注解:
@NotBlank(message =) 验证字符串非null,且trim后长度必须大于0
@Email 被注释的元素必须是电子邮箱地址
@Length(min=,max=) 被注释的字符串的大小必须在指定的范围内
@NotEmpty 被注释的字符串的必须非空
@Range(min=,max=,message=) 被注释的元素必须在合适的范围内
@AssertFalse 校验false
@AssertTrue 校验true
@DecimalMax(value=,inclusive=) 小于等于value,
inclusive=true,是小于等于
@DecimalMin(value=,inclusive=) 与上类似
@Max(value=) 小于等于value
@Min(value=) 大于等于value
@NotNull 检查Null
@Past 检查日期
@Pattern(regex=,flag=) 正则
@Size(min=, max=) 字符串,集合,map限制大小
@Valid 对po实体类进行校验
三、代码示例
1、Controller添加@Valid或者@Validated都可以
@RestController @RequestMapping("/") public class UserController { @RequestMapping("testValidation") public String testValidation(@Valid @RequestBody UserRequest userRequest) { return "success"; } }
2、实体类添加校验注解
import java.io.Serializable; import javax.validation.constraints.Max; import javax.validation.constraints.Pattern; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotEmpty; import org.hibernate.validator.constraints.Range; import lombok.Data; @Data public class UserRequest implements Serializable{ private static final long serialVersionUID = 1L; @NotEmpty(message="用户id不能为空") private String userId; @Email(message="非法邮件地址") private String email; @Pattern(regexp="^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X)$",message="身份证号不合规") private String cardNo; @Length(min=1,max=10,message="昵称长度必须在1~10") private String nickName; @Range(min=0,max=2,message="非法性别") private String sex; @Max(value=100,message="非法年龄") private int age; }
3、添加全局异常拦截
@Slf4j @RestControllerAdvice public class GlobalExceptionHandler { /** * 参数合法性校验异常 * @param exception * @return */ @ResponseBody @ExceptionHandler(MethodArgumentNotValidException.class) public String validationBodyException(MethodArgumentNotValidException exception){ StringBuffer buffer = new StringBuffer(); BindingResult result = exception.getBindingResult(); if (result.hasErrors()) { List<ObjectError> errors = result.getAllErrors(); errors.forEach(p ->{ FieldError fieldError = (FieldError) p; log.error("Data check failure : object{"+fieldError.getObjectName()+"},field{"+fieldError.getField()+ "},errorMessage{"+fieldError.getDefaultMessage()+"}"); buffer.append(fieldError.getDefaultMessage()).append(","); }); } BaseResponse response = new BaseResponse(BusinessCodeEnum.INVALID_PARAM); response.setRespMsg(buffer.toString().substring(0, buffer.toString().length()-1)); return JSONObject.toJSONString(response); } }
四、备注