小言_互联网的博客

SpringBoot使用validator进行参数校验

337人阅读  评论(0)
  1. @Validated、@Valid和BindingResult

Bean Validation是Java定义的一套基于注解的数据校验规范,比如@Null、@NotNull、@Pattern等,它们位于 javax.validation.constraints这个包下。

hibernate validator是对这个规范的实现,并增加了一些其他校验注解,如 @NotBlank、@NotEmpty、@Length等,它们位于org.hibernate.validator.constraints这个包下。

依赖

hibernate validator框架已经集成在 spring-boot-starter-web中,所以无需再添加其他依赖。如果不是Spring Boot项目,需要添加如下依赖。

@Valid和@Validated 区别

Spring Validation验证框架对参数的验证机制提供了@Validated(Spring's JSR-303规范,是标准JSR-303的一个变种)。

javax提供了@Valid,配合BindingResult可以直接提供参数验证结果(标准JSR-303规范)。

@Validation对@Valid进行了二次封装,在使用上并没有区别,但在分组、注解位置、嵌套验证等功能上有所不同

  • 分组

@Validated:提供了一个分组功能,可以在入参验证时,根据不同的分组采用不同的验证机制。

@Valid:没有分组校验的功能。

  • 注解地方

@Validated:用在类型、方法和方法参数上(类, 方法, 参数)。但不能用于成员属性。

@Valid:可以用在方法、构造函数、方法参数和成员属性上(方法, 构造器, 参数,字段, 泛型),可以用@Valid实现嵌套验证

两者是否能用于成员属性(字段)上直接影响能否提供嵌套验证的功能
如A类中引用B类,且A、B二类都有内部校验,为了使B类也生效,在A类中引用B类时,在B类变量上加@Valid注解,如果B类为集合等类型且不能为空还需要再加@NotEmpty。

BindingResult

BindingResult用在实体类校验信息返回结果绑定。

该类作为方法入参,要写在实体对象后面。


   
  1. @PostMapping("/menus")
  2. public Result addMenu (@RequestBody @Valid Menu menu, BindingResult result) {}
  1. 规则注解

validator内置注解

hibernate validator扩展注解

分类

空与非空

注解

支持Java类型

说明

@Null

Object

为null

@NotNull

Object

不为null

@NotBlank

CharSequence

不为null,且必须有一个非空格字符

@NotEmpty

CharSequence、Collection、Map、Array

不为null,且不为空(length/size>0)

Boolean

注解

支持Java类型

说明

备注

@AssertTrue

boolean、Boolean

为true

为null有效

@AssertFalse

boolean、Boolean

为false

为null有效

日期

注解

支持Java类型

说明

备注

@Future

Date、

Calendar、

Instant、

LocalDate、

LocalDateTime、

LocalTime、

MonthDay、

OffsetDateTime、

OffsetTime、

Year、

YearMonth、

ZonedDateTime、

HijrahDate、

JapaneseDate、

MinguoDate、

ThaiBuddhistDate

验证日期为当前时间之后

为null有效

@FutureOrPresent

Date、

Calendar、

Instant、

LocalDate、

LocalDateTime、

LocalTime、

MonthDay、

OffsetDateTime、

OffsetTime、

Year、

YearMonth、

ZonedDateTime、

HijrahDate、

JapaneseDate、

MinguoDate、

ThaiBuddhistDate

验证日期为当前时间或之后

为null有效

@Past

Date、

Calendar、

Instant、

LocalDate、

LocalDateTime、

LocalTime、

MonthDay、

OffsetDateTime、

OffsetTime、

Year、

YearMonth、

ZonedDateTime、

HijrahDate、

JapaneseDate、

MinguoDate、

ThaiBuddhistDate

验证日期为当前时间之前

为null有效

@PastOrPresent

Date、

Calendar、

Instant、

LocalDate、

LocalDateTime、

LocalTime、

MonthDay、

OffsetDateTime、

OffsetTime、

Year、

YearMonth、

ZonedDateTime、

HijrahDate、

JapaneseDate、

MinguoDate、

ThaiBuddhistDate

验证日期为当前时间或之前

为null有效

数值

注解

支持Java类型

说明

备注

@Max

BigDecimal、BigInteger,

byte、short、int、long以及包装类

小于或等于

为null有效

@Min

BigDecimal、BigInteger,

byte、short、int、long以及包装类

大于或等于

为null有效

@DecimalMax

BigDecimalBigInteger、CharSequence,

byte、short、int、long以及包装类

小于或等于

为null有效

@DecimalMin

BigDecimal、BigIntegerCharSequence,

byte、short、int、long以及包装类

大于或等于

为null有效

@Negative

BigDecimal、BigInteger,

byte、short、int、long、float、double以及包装类

负数

为null有效,0无效

@NegativeOrZero

BigDecimal、BigInteger,

byte、short、int、long、float、double以及包装类

负数或零

为null有效

@Positive

BigDecimal、BigInteger,

byte、short、int、long、float、double以及包装类

正数

为null有效,0无效

@PositiveOrZero

BigDecimal、BigInteger,

byte、short、int、long、float、double以及包装类

正数或零

为null有效

@Digits(integer = 3, fraction = 2)

BigDecimal、BigInteger、CharSequence,

byte、short、int、long以及包装类

整数位数和小数位数上限

为null有效

@Length

String

字符串长度范围

@Length

@Range

数值类型和String

指定范围

@Range

其他

注解

支持Java类型

说明

备注

@Pattern

CharSequence

匹配指定的正则表达式

为null有效

@Email

CharSequence

邮箱地址

为null有效,默认正则 '.*'

@Size

CharSequence、Collection、Map、Array

大小范围(length/size>0)

为null有效

@URL

URL地址验证

@URL

  1. 使用

单参数校验

需要在参数前添加注解,而且controller类上必须添加@Validated注解。


   
  1. @RestController
  2. @RequestMapping("/menu")
  3. @Validated // 单参数校验需要加的注解
  4. public class SysMenuController {
  5.     @DeleteMapping("/menus")
  6.     public Result deleteMenu (@NotNull(message = "id不能为空") Long id) {
  7.     }
  8. }

对象参数校验

先在对象的校验属性上添加注解,然后在Controller方法的对象参数前添加@Valid、@Validated


   
  1. // 对象
  2. public class Menu {
  3. private Long menuId;
  4. @NotNull(message =parentId不能为空")
  5. private Long parentId;
  6. }

   
  1. @PostMapping("/menus")
  2. public Result addMenu (@RequestBody @Valid Menu menu, BindingResult result) {
  3. }

对象嵌套


   
  1. // 对象
  2. public class PagedQueryReqBody<T> {
  3. private Integer page_no;
  4. private Integer page_row_no;
  5. @NotNull
  6. private String page_flg;
  7. @Valid
  8. private T data_request;
  9. }
  10. public class DataReqPQ {
  11. @NotNull
  12. private String car_no;
  13. }

   
  1. // 接口
  2. @PostMapping(value = "/queryParameter")
  3. public Result queryParameter (@RequestBody @Validated PagedQueryReqBody<DataReqPQ> requestMsg, BindingResult result){
  4. }

分组校验

新建组

Validated有自己默认的组 Default.class


   
  1. public interface Update {
  2. }
  3. public interface Add extends Default {
  4. }

   
  1. // 对象
  2. public class User {
  3. @NotBlank(message = "id不能为空",groups = {Update.class})
  4.     private String id;
  5. private String name;
  6. @NotBlank(message = "密码不能为空",groups = {Add.class})
  7. private String password;
  8. }
id属性的校验属于Update分组的校验
password属性的校验属于Add、Default分组的校验

使用分组

使用默认分组:Add分组继承Default,所以校验password,不校验id


   
  1. @PostMapping("/addUser")
  2. public Resp addUser (@Validated @RequestBody User uer) {
  3. }

使用Update分组:只校验id,不校验password


   
  1. @PostMapping("/updateUser")
  2. public Resp updateUser (@Validated(Update.class) @RequestBody User user) {
  3. }
  1. 异常处理

全局异常处理类

缺少参数抛出的异常是MissingServletRequestParameterException

单参数校验失败后抛出的异常是ConstraintViolationException

get请求的对象参数校验失败后抛出的异常是BindException

post请求的对象参数校验失败后抛出的异常是MethodArgumentNotValidException

不同异常对象的结构不同,对异常消息的提取方式也就不同。


   
  1. @Slf4j
  2. @RestControllerAdvice
  3. public class ExceptionAdvice {
  4. @ResponseBody
  5. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 设置状态码为500
  6. @ExceptionHandler(MethodArgumentNotValidException.class)
  7. public String postExceptionHandler (MethodArgumentNotValidException e){
  8. log.error( "执行异常",e);
  9.         BindingResult exceptions = e.getBindingResult();
  10.         if (exceptions.hasErrors()) {}
  11. }
  12. @ResponseBody
  13. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 设置状态码为500
  14.     @ExceptionHandler(ConstraintViolationException.class)
  15. public String paramExceptionHandler (ConstraintViolationException e){
  16. log.error( "执行异常",e);
  17. }
  18. }

BindingResult异常

Controller方法的中处理


   
  1. @PostMapping("addUser")
  2. public Result addUser (@RequestBody @Valid User user,BindingResult result){
  3.     //校验到错误
  4.     if (result.hasErrors()) {
  5.     //获得错误信息列表
  6.     List<String> errMsgs = result.getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(toList());
  7.     String lists = StringUtils.join(lists, ";");
  8.     return new Result(“” "", lists);
  9.     }
  10. return new Result(“”, "", null);
  11. }

AOP校验


   
  1. /**
  2. *将此注解加在需要进行参数校验的方法上,
  3. */
  4. @Target(ElementType.METHOD)
  5. @Retention(RetentionPolicy.RUNTIME)
  6. public @interface ParamValid {
  7. }

   
  1. @Aspect
  2. @Component
  3. public class ParamValidAspect {
  4. private static final Logger log = LoggerFactory.getLogger(ParamValidAspect.class);
  5. @Before("@annotation(paramValid)")
  6. public void paramValid (JoinPoint point, ParamValid paramValid){
  7.     Object[] paramObj = point.getArgs();
  8.     if (paramObj.length > 0){
  9.     Arrays.stream(paramObj).forEach(
  10.     e ->{
  11.         if (e instanceof BindingResult) {
  12.         BindingResult result = (BindingResult) e;
  13.         Result errorMap = this.validRequestParams(result);
  14.         if (errorMap != null){
  15.         ServletRequestAttributes res = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  16.         HttpServletResponse response = res.getResponse();
  17.         response.setCharacterEncoding( "UTF-8");
  18.         response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
  19.         response.setStatus(HttpStatus.BAD_REQUEST.value());
  20.         OutputStream output = null;
  21.         try {
  22.         output = response.getOutputStream();
  23.         String error = objectMapper.writeValueAsString(errorMap);
  24.         //响应错误信息
  25.         output.write(error.getBytes( "UTF-8"));
  26.         } catch (IOException e){
  27.        log.error(e.getMessage());
  28.       } finally{
  29.        try{
  30.         if (output != null){
  31.         output.close();
  32.         }
  33.        } catch (IOException e) {
  34.         log.error(e.getMessage());
  35.        }
  36.        }
  37.       }
  38. }
  39. });
  40. }
  41. }
  42. /**
  43. * 校验
  44. */
  45. private Result validRequestParams (BindingResult result) {
  46. if (result.hasErrors()) {
  47.             List<String> errMsgs = result.getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(toList());
  48.             String lists = StringUtils.join(lists, ";");
  49. return new Result( "", "", lists);
  50. }
  51. return null;
  52. }
  53. }

转载:https://blog.csdn.net/csdnzhang365/article/details/129141189
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场