java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot jsr303校验

SpringBoot使用jsr303校验的实现

作者:guomz

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

依赖添加

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

一些较老版本的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;
}

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

@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<TPointDeduct> 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<TPointDeduct> 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<Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request){
  List<FieldError> 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<Object> 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校验内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文