入参校验(主要讲用法,原理后面研究)
@Valid与@Validated都可以触发校验。
@Valid是jdk自带的,刻意用在方法、构造函数、方法参数和成员属性(字段)上。
@Validated是Spring对@Valid的增强,支持分组校验,可以用在类型、方法和方法参数上。但是不能用在成员属性(字段)上。
用法
@Valid与@Validated都可以使用,只是校验失败抛的异常不同
@Data
public class UserTestVO {
@NotNull(message = "name不能为空")
private String name;
@Max(value = 100, message = "年龄不能大于100岁")
private Integer age;
}
@RestController
public class ItemController {
//校验失败抛MethodArgumentNotValidException
@PostMapping("/post")
public HttpResultToWeb save(@Validated @RequestBody UserTestVO userTestVO) {
return HttpResultWrapper.wrap();
}
//校验失败抛MethodArgumentNotValidException
@PostMapping("/postA")
public HttpResultToWeb saveA(@Valid @RequestBody UserTestVO userTestVO) {
return HttpResultWrapper.wrap();
}
//校验失败抛MethodArgumentNotValidException
@GetMapping("/getBody")
public HttpResultToWeb saveGet(@Valid @RequestBody UserTestVO userTestVO) {
return HttpResultWrapper.wrap();
}
//校验失败抛BindException
@GetMapping("/getBodyA")
public HttpResultToWeb saveGetA(@Valid UserTestVO userTestVO) {
return HttpResultWrapper.wrap();
}
//校验失败抛BindException
@GetMapping("/getBodyB")
public HttpResultToWeb saveGetB(@Validated UserTestAVO userTestVO) {
return HttpResultWrapper.wrap();
}
}
@RestController
@Validated //必须加在类上,加在方法上不会生效
public class ItemController {
//校验失败抛ConstraintViolationException
@GetMapping("/getParam")
public HttpResultToWeb saveGetParam(@RequestParam(name = "age") @Max(value = 100,message = "最大为100") Integer age) {
return HttpResultWrapper.wrap();
}
}
校验失败可能会抛三个不同的异常
BindException
MethodArgumentNotValidException
ConstraintViolationException
具体哪个情况抛哪种异常代码上有注释
异常捕获
通过@ControllerAdvice统一捕获
@ControllerAdvice()
public class ControllerExceptionResolver {
/**
* GET请求 使用@Valid 无@@RequestBody 验证请求失败后抛出的异常
*
* @param e BindException
* @return HttpResultToWeb
*/
@ExceptionHandler(BindException.class)
@ResponseBody
public HttpResultToWeb handleBindException(BindException e) {
String message = e.getBindingResult().getAllErrors().stream().map(
DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
return HttpResultWrapper.wrap(HttpStatusCode.PARAM_ERROR, message);
}
/**
* GET与POST请求 使用@Validated 验证请求body失败后抛出的异常
*
* @param e MethodArgumentNotValidException
* @return HttpResultToWeb
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public HttpResultToWeb handleArgumentNotValidException(MethodArgumentNotValidException e) {
String message = e.getBindingResult().getAllErrors().stream().map(
DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
return HttpResultWrapper.wrap(HttpStatusCode.PARAM_ERROR, message);
}
/**
* GET请求 在类上使用@Validated请求参数校验失败抛出的异常
*
* @param e ConstraintViolationException
* @return HttpResultToWeb
*/
@ExceptionHandler(ConstraintViolationException.class)
@ResponseBody
public HttpResultToWeb handleConstraintViolationException(ConstraintViolationException e) {
String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(
Collectors.joining());
return HttpResultWrapper.wrap(HttpStatusCode.PARAM_ERROR, message);
}
}
嵌套验证
public class Item {
@NotNull(message = "id不能为空")
@Min(value = 1, message = "id必须为正整数")
private Long id;
@NotNull(message = "props不能为空")
@Size(min = 1, message = "至少要有一个属性")
private List<Prop> props;
}
public class Prop {
@NotNull(message = "pid不能为空")
@Min(value = 1, message = "pid必须为正整数")
private Long pid;
@NotNull(message = "vid不能为空")
@Min(value = 1, message = "vid必须为正整数")
private Long vid;
@NotBlank(message = "pidName不能为空")
private String pidName;
@NotBlank(message = "vidName不能为空")
private String vidName;
}
@RestController
public class ItemController {
@RequestMapping("/item/add")
public void addItem(@Validated Item item, BindingResult bindingResult) {
doSomething();
}
}
只会对props校验@NotNull与@Size无法对集合对象Prop进行属性校验
如要对Prop进行校验要将代码改成如下:
public class Item {
@NotNull(message = "id不能为空")
@Min(value = 1, message = "id必须为正整数")
private Long id;
@Valid // 嵌套验证必须用@Valid
@NotNull(message = "props不能为空")
@Size(min = 1, message = "props至少要有一个自定义属性")
private List<Prop> props;
}