Spring Boot使用JSR-380进行校验

介绍

JSR-380是 J2EE 的一个规范,用于校验实体属性,它是JSR-303的升级版,在 Spring Boot 中可以基于它优雅实现参数校验。

示例

在没有使用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 验证日期值是未来(包括现在)
#Java##程序员##计算机##Java学习#
全部评论
感谢大神的完美分享,已收藏
点赞 回复 分享
发布于 2022-07-27 11:09
期待楼主继续分享
点赞 回复 分享
发布于 2022-07-28 20:38

相关推荐

点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务