如何优雅的处理SpringBoot Web中的异常

本文最后更新于:2022年7月3日 下午

如何优雅的处理SpringBoot Web中的异常

大家在开发Web项目中的时候总是会遇到一些让人很麻烦的参数校验和一些固定的返回格式,如果我们在每个方法中都加入这些代码就很麻烦,大大降低我们的开发效率。今天我们就看一下如何进行优雅的异常抛出,来做个记录吧。

首先定义一个异常的接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
public interface BasicException {
/**
* 错误码
* @return
*/
String getResultCode();

/**
* 错误描述
* @return
*/
String getResultMsg();
}

然后我们定义一些常见的基础的枚举异常

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
public enum CommonEnum implements BasicException {
/**
* 基础的一些错误定义。
*/
SUCCESS("200", "成功!"),
BODY_NOT_MATCH("400", "请求的数据格式不符!"),
SIGNATURE_NOT_MATCH("401", "请求的数字签名不匹配!"),
NOT_FOUND("404", "未找到该资源!"),
INTERNAL_SERVER_ERROR("500", "服务器内部错误!"),
SERVER_BUSY("503", "服务器正忙,请稍后再试!");

private String resultCode;
private String resultMsg;

CommonEnum(String resultCode, String resultMsg) {
this.resultCode = resultCode;
this.resultMsg = resultMsg;
}

@Override
public String getResultCode() {
return resultCode;
}

@Override
public String getResultMsg() {
return resultMsg;
}
}

再来一个自定义异常的包装类。

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
public class CustomException extends RuntimeException implements BasicException {

private String errorCode;
private String errorMsg;

public CustomException() {
super();
}

public CustomException(BasicException exception) {
super(exception.getResultCode());
this.errorCode = exception.getResultCode();
this.errorMsg = exception.getResultMsg();
}

public CustomException(String errorMsg) {
this.errorMsg = errorMsg;
}

public CustomException(String errorCode, String errorMsg) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}

public CustomException(String errorCode, String errorMsg, Throwable cause) {
super(cause);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}

public String getErrorCode() {
return errorCode;
}

public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}

public String getErrorMsg() {
return errorMsg;
}

public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}

@Override
public String getResultCode() {
return this.errorCode;
}

@Override
public String getResultMsg() {
return this.errorMsg;
}
}

再进行一下异常的全局捕获抛出。

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
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* 捕捉自定义异常,减少代码量
*
* @param request
* @param exception
* @return
*/
@ExceptionHandler(CustomException.class)
public ResultBody handleCustomException(HttpServletRequest request, CustomException exception) {
log.error("发生异常");
return ResultBody.error(exception.getErrorCode(), exception.getMessage());
}

@ExceptionHandler(value = NullPointerException.class)
public ResultBody exceptionHandler(HttpServletRequest req, NullPointerException e) {
log.error("发生空指针异常!原因是:", e);
return ResultBody.error(CommonEnum.BODY_NOT_MATCH);
}

@ExceptionHandler(ConstraintViolationException.class)
public ResultBody constraintViolationExceptionHandle(HttpServletRequest req, ConstraintViolationException e) {
String message = e.getMessage();
String[] messages = message.split(": ");
return ResultBody.error(messages[messages.length - 1]);
}
}

image-20201111230227278

image-20201111230302968

image-20201111230506476

image-20201111230816506


如何优雅的处理SpringBoot Web中的异常
https://dlddw.xyz/2022/07/01/优雅的异常处理/
作者
Deepblue
发布于
2022年7月1日
许可协议