介绍 JSR-380 是 J2EE 的一个规范,用于校验实体属性,它是 JSR-303 的升级版,在 Spring Boot 中可以基于它优雅实现参数校验。 !--more-- 示例 在没有使用 JSR-380 之前,我们一般都会将参数校验
          介绍
JSR-380 是 J2EE 的一个规范,用于校验实体属性,它是 JSR-303 的升级版,在 Spring Boot 中可以基于它优雅实现参数校验。
<!--more-->
示例
在没有使用 JSR-380 之前,我们一般都会将参数校验硬编码在 controller 类中,示例:
public Result add(@RequestBody User user){
  if(StringUtils.isBlank(user.getName())){
    return Result.error("用户名不能为空");
  }
  // ...
}
而使用 JSR-380 只需要通过添加对应的注解即可实现校验,示例:
@Data
public class User{
  @NotBlank
  private String name;
  private Integer age;
}
public Result register(@Validated @RequestBody User user){
  // ...
}
这样看起来代码是不是清爽了很多,只需要在需要校验的字段上加上对应的校验注解,然后对需要校验的地方加上 @Validated 注解,然后框架就会帮我们完成校验。
通过全局异常自定义错误响应
框架校验失败之后会抛出异常,需要捕获这个异常然后来自定义校验不通过的错误响应,这里直接贴代码,兼容 @RequestBody 、 @ModelAttribute 、 @RequestParam 三种入参的校验:
@ControllerAdvice
public class GlobalExceptionHandler {
  @ExceptionHandler(value = {MethodArgumentNotValidException.class, BindException.class})
  public ResponseEntity<Result> methodArgumentNotValidHandler(HttpServletRequest request, Exception e) {
    BindingResult bindingResult;
    if (e instanceof MethodArgumentNotValidException) {
      //@RequestBody参数校验
      bindingResult = ((MethodArgumentNotValidException) e).getBindingResult();
    } else {
      //@ModelAttribute参数校验
      bindingResult = ((BindException) e).getBindingResult();
    }
    FieldError fieldError = bindingResult.getFieldError();
    return ResponseEntity.ok(Result.fail(Result.CODE_PARAMS_INVALID, "[" + fieldError.getField() + "]" + fieldError.getDefaultMessage()));
  }
  //@RequestParam参数校验
  @ExceptionHandler(value = {ConstraintViolationException.class, MissingServletRequestParameterException.class})
  public ResponseEntity<Result> constraintViolationHandler(Exception e) {
    String field;
    String msg;
    if (e instanceof ConstraintViolationException) {
      ConstraintViolation<?> constraintViolation = ((ConstraintViolationException) e).getConstraintViolations().stream().findFirst().get();
      List<Path.Node> pathList = StreamSupport.stream(constraintViolation.getPropertyPath().spliterator(), false)
          .collect(Collectors.toList());
      field = pathList.get(pathList.size() - 1).getName();
      msg = constraintViolation.getMessage();
    } else {
      // 这个不是JSR标准返回的异常,要自定义提示文本
      field = ((MissingServletRequestParameterException) e).getParameterName();
      msg = "不能为空";
    }
    return ResponseEntity.ok(Result.fail(Result.CODE_PARAMS_INVALID, "[" + field + "]" + msg));
  }
}
然后再访问一下接口,可以看到错误提示已经按自定义的规范显示了:

可以看到都不需要写任何提示文本就可以完成校验和提示,上图的 不能为空 是框架内置的 I18N 国际化支持,每个注解都内置相应的提示模板。
常用校验注解
  
    
       
  
  注解 
      描述 
    
本文完整代码放在 github 。
Java Bean Validation Basics
JSR-380 规范
到此这篇关于Spring Boot使用JSR-380进行校验的文章就介绍到这了,更多相关Spring Boot使用JSR-380校验内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
