微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringBoot使用jsr303校验的实现

这篇文章主要介绍了SpringBoot使用JSR303校验的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

依赖添加

org.springframework.bootspring-boot-starter-validation

一些较老版本的SpringBoot需要添加相关依赖,我使用的2.1.4发行版不用这个操作。

验证使用对象接收参数的情况

public class PointDeductSetRequest { private Long id; @NotBlank(message = "租户id为空") private String tenantId; private Integer status; @NotNull private Integer pointValue; @NotNull private Integer deductValue; @NotBlank(message = "操作员id为空") private String operator; }

首先在需要验证的对象的对应字段上方加上校验注解,以下为一些常用注解:

@Null 限制只能为null

@NotNull 限制必须不为null

@AssertFalse 限制必须为false

@AssertTrue 限制必须为true

@DecimalMax(value) 限制必须为一个不大于指定值的数字

@DecimalMin(value) 限制必须为一个不小于指定值的数字

@Digits(integer,fraction) 限制必须为一个小数,且整数部分的位数不能超过integer,小数部分的位数不能超过fraction

@Future 限制必须是一个将来的日期

@Max(value) 限制必须为一个不大于指定值的数字

@Min(value) 限制必须为一个不小于指定值的数字

@Past 限制必须是一个过去的日期

@Pattern(value) 限制必须符合指定的正则表达式

@Size(max,min) 限制字符长度必须在min到max之间

@Past 验证注解的元素值(日期类型)比当前时间早

@NotEmpty 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0)

@NotBlank 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格

@Email 验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式

@RequestMapping(value = "/deduct", method = RequestMethod.POST) public BusinessResponse setPointDeduct(@RequestBody @Valid PointDeductSetRequest request){ pointDeductService.setPointDeductRule(request); return new BusinessResponse(ResponseEnum.OK); }

之后在controller方法的对象参数前加@Valid注解。

校验使用单个参数接受的情况

@RequestMapping(value = "/deduct", method = RequestMethod.GET) public PageResponse getPointDeductList(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "pageSize", required = false) Integer pageSize, @RequestParam(value = "tenantId", required = false) @NotBlank(message = "租户id为空") String tenantId, @RequestParam(value = "status", required = false) Integer status){ PageResponse response = pointDeductService.getPointDeductList(page, pageSize, tenantId, status); response.setCodeMsg(ResponseEnum.OK); return response; }首先需要在controller类上加@Validated注解,之后在方法中需要校验的参数前加上对应的校验注解进行校验。对校验产生的异常的捕获定义全局异常处理类并用@ControllerAdvice标注,由于对象和单个参数因校验产生的异常类型不同,因此需要分别处理。对于对象作为接收前端请求的情况,因校验产生的异常类型为MethodArgumentNotValidException,示例方法如下:/** * 捕获303对于body中的对象字段校验 * @param e * @param request * @return */@ExceptionHandler(MethodArgumentNotValidException.class) @ResponseBody ResponseEntity handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request){ List fieldErrors = e.getBindingResult().getFieldErrors(); if (fieldErrors != null && !fieldErrors.isEmpty()){ String message = fieldErrors.get(0).getDefaultMessage(); log.error(message, e); } HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; HttpHeaders headers = new HttpHeaders(); Response response = new Response(); response.setCode(ResponseEnum.FORMAT_ERROR.code()); response.setMessage(ResponseEnum.FORMAT_ERROR.message()); return new ResponseEntity(response, headers, httpStatus); }对于使用单个参数接受前端请求,因校验产生的异常类为ConstraintViolationException,示例方法如下:/** * 捕获303对于request param单个参数的校验 * @param e * @param request * @return */@ExceptionHandler(ConstraintViolationException.class) @ResponseBody ResponseEntity handleConstraintViolationException(ConstraintViolationException e, HttpServletRequest request){ HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; HttpHeaders headers = new HttpHeaders(); Response response = new Response(); response.setCode(ResponseEnum.FORMAT_ERROR.code()); response.setMessage(ResponseEnum.FORMAT_ERROR.message()); return new ResponseEntity(response, headers, httpStatus); }到此这篇关于SpringBoot使用JSR303校验的实现的文章就介绍到这了,更多相关SpringBoot JSR303校验内容搜索编程之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程之家!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐