Spring MVC 全局异常处理

本文最后更新于:2024年9月8日 晚上

Spring MVC 全局异常处理

  • SpringBoot中,@ControllerAdvice 即可开启全局异常处理,使用该注解表示开启了全局异常的捕获,我们只需在自定义一个方法使用@ExceptionHandler注解然后定义捕获异常的类型即可对这些捕获的异常进行统一的处理。

自定义异常类

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(value = AccessDeniedException.class)
public Result<String> handler(AccessDeniedException e) {
log.error("未授权异常:----------------{}", e.getMessage(), e);
return ResultUtil.error(403, "用户未授权", e.getMessage());
}

@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(value = NoHandlerFoundException.class)
public Result<String> handler(NoHandlerFoundException e) {
log.error("未找到资源:----------------{}", e.getMessage(), e);
return ResultUtil.error(404, "未找到资源", e.getMessage());
}

@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler(value = HttpMediaTypeNotSupportedException.class)
public Result<String> handler(HttpMediaTypeNotSupportedException e) {
log.error("参数类型异常:----------------{}", e.getMessage(), e);
return ResultUtil.error(415, "参数类型异常", e.getMessage());
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = BusinessException.class)
public Result<String> handler(BusinessException e) {
log.error("业务异常------------------{}", e.getErrorMessage(), e);
return ResultUtil.error(e.getErrorCode(), "业务异常", e.getErrorMessage());
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public Result<String> handler(MethodArgumentNotValidException e) {
log.error("实体校验异常:----------------{}", e.getMessage(), e);
BindingResult bindingResult = e.getBindingResult();
ObjectError objectError = bindingResult.getAllErrors().stream().findFirst().get();
return ResultUtil.error("实体校验异常", objectError.getDefaultMessage());
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = IllegalArgumentException.class)
public Result<String> handler(IllegalArgumentException e) {
log.error("参数异常:----------------{}", e.getMessage(), e);
return ResultUtil.error("参数异常", e.getMessage());
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = Exception.class)
public Result<String> handler(Exception e) {
log.error("服务器异常:----------------{}", e.getMessage(), e);
return ResultUtil.error("服务器异常", e.getMessage());
}

}

定义全局异常处理器

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
36
37
38
39
40
41
42
43
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = BusinessException.class)
public Result handler(BusinessException e) {
log.error("业务异常------------------{}", e.getErrorMessage());
return ResultUtil.error(e.getErrorCode(), e.getErrorMessage());
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public Result handler(MethodArgumentNotValidException e) {
log.error("实体校验异常:----------------{}", e.getMessage());
BindingResult bindingResult = e.getBindingResult();
ObjectError objectError = bindingResult.getAllErrors().stream().findFirst().get();
return ResultUtil.error(objectError.getDefaultMessage());
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = IllegalArgumentException.class)
public Result handler(IllegalArgumentException e) {
log.error("参数异常:----------------{}", e.getMessage());
return ResultUtil.error(e.getMessage());
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = RuntimeException.class)
public Result<String> handler(RuntimeException e) {
log.error("运行时异常:----------------{}", e.getMessage());
return ResultUtil.error("服务器异常");
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = Exception.class)
public Result<String> handler(Exception e) {
log.error("服务器异常:----------------{}", e.getMessage());
return ResultUtil.error("服务器异常");
}

}