关于java:Spring-Boot使用JSR380进行校验

20次阅读

共计 2653 个字符,预计需要花费 7 分钟才能阅读完成。

介绍

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 国际化反对,每个注解都内置相应的提醒模板。

罕用校验注解

注解 形容
@NotNull 验证值不为 null
@AssertTrue 验证值为 true
@Size 验证值的长度介于 min 和 max 之间,可利用于 String、Collection、Map 和数组类型
@Min 验证值不小于该值
@Max 验证值不大于该值
@Email 验证字符串是无效的电子邮件地址
@NotEmpty 验证值不为 null 或空,可利用于 String、Collection、Map 和数组类型
@NotBlank 验证字符串不为 null 并且不是空白字符
@Positive 验证数字为负数
@PositiveOrZero 验证数字为负数(包含 0)
@Negative 验证数字为正数
@NegativeOrZero 验证数字为正数(包含 0)
@Past 验证日期值是过来
@PastOrPresent 验证日期值是过来(包含当初)
@Future 验证日期值是将来
@FutureOrPresent 验证日期值是将来(包含当初)

附录

本文残缺代码放在 github。

  • Java Bean Validation Basics
  • JSR-380 标准

我是 MonkeyWie,欢送扫码???????? 关注!不定期在公众号中分享JAVAGolang 前端 dockerk8s 等干货常识。

正文完
 0