原理

  • 依赖
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
  • 配置handler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@RestControllerAdvice
@Slf4j
public class exceptionHander {

/**
* 抓取自带注解的报错(校验@NotNull@NotBlank 等)
* @param e 异常
* @return 返回信息
*/
@ExceptionHandler(BindException.class)
public ReturnDto handleError(BindException e) {
return ReturnDto.error(e.getBindingResult().getFieldError().getDefaultMessage());
}

/**
* 抓取自带注解的报错
* @param e 异常
* @return 返回信息
*/
@ExceptionHandler(ConstraintViolationException.class)
public ReturnDto handleError(ConstraintViolationException e) {
return ReturnDto.error(e.getMessage());
}

/**
* 抓取自定义注解的报错(或校验@Length 等)
* @param e 异常
* @return 返回信息
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ReturnDto exceptionHandler(MethodArgumentNotValidException e) {
return ReturnDto.error(e.getBindingResult().getFieldError().getDefaultMessage());
}

}
  • 实体类
1
2
3
4
5
6
7
8
@ApiModelProperty("标题")
@NotNull(message = "标题不能为空")
private String title;

@ApiModelProperty("信息")
@NotNull(message = "信息不能为空")
@Length(min=0, max=5)
private String message;