阅读 55 SEO

springboot 自定义异常并捕获异常返给前端的实现代码

在开发中,如果用try catch的方式,每个方法都需要单独实现,为了方便分类异常,返回给前端,采用了@ControllerAdvice注解和继承了RuntimeException的方式来实现,具体实现内容跟随小编一起看看吧

背景

在开发中,如果用try catch的方式,每个方法都需要单独实现,为了方便分类异常,返回给前端,采用了@ControllerAdvice注解和继承了RuntimeException的方式来实现。

实现内容

捕获了三类异常
1.业务异常

          BusinessException

2.系统异常 

        SystemException

3.其他异常

        利用@ExceptionHandler(RuntimeException.class)去捕获

ExceptionAdvice类捕获以上三类异常,并返回自定义类型格式数据

实现代码

业务异常BusinessException类实现方式,继承RuntimeException

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
public class BusinessException extends  RuntimeException {
    /**
     * 错误编码
     */
    private String code;
  
    public BusinessException() {
        super();
    }
  
    public BusinessException(String message) {
        super(message);
    }
  
    public BusinessException(String code, String message) {
        super(message);
        this.code = code;
    }
  
    public BusinessException(Throwable cause) {
        super(cause);
    }
  
    public BusinessException(String message, Throwable cause) {
        super(message, cause);
    }
  
    public BusinessException(String message, Throwable cause,
                             boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
  
    public String getCode() {
        return code;
    }
  
    public void setCode(String code) {
        this.code = code;
    }
  
    @Override
    public String getMessage() {
        return super.getMessage();
    }
  
    @Override
    public String toString() {
        return this.code + ":" + this.getMessage();
    }
}

系统异常SystemException类实现方式,继承RuntimeException,同业务异常类的实现方式一样

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
public class SystemException extends  RuntimeException {
    /**
     * 错误编码
     */
    private String code;
  
    public SystemException() {
        super();
    }
  
    public SystemException(String message) {
        super(message);
    }
  
    public SystemException(String code, String message) {
        super(message);
        this.code = code;
    }
  
    public SystemException(Throwable cause) {
        super(cause);
    }
  
    public SystemException(String message, Throwable cause) {
        super(message, cause);
    }
  
    public SystemException(String message, Throwable cause,
                           boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
  
    public String getCode() {
        return code;
    }
  
    public void setCode(String code) {
        this.code = code;
    }
  
    @Override
    public String getMessage() {
        return super.getMessage();
    }
  
    @Override
    public String toString() {
        return this.code + ":" + this.getMessage();
    }
}

ExceptionAdvice类,采用增强Controller注解 @ControllerAdvice的方式来实现


1.方法名称和返回类型都可以根据自己需要定义

2.采用注解@ExceptionHandler,就是捕获的异常类型,我们只需要把需要捕获异常类型写进来就好

 ExceptionAdvice 具体代码实现如下:

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
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
  
@ControllerAdvice
public class ExceptionAdvice {
    public static Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);
  
    @ResponseBody
    @ExceptionHandler(SystemException.class)
    public Result handleException(Exception e) {
        logger.error("系统异常信息:", e);
        Result result = new Result();
        if (e instanceof BusinessException) {
            e = (BusinessException) e;
            result.setCode(((BusinessException) e).getCode());
        }
        result.setFailed(e.getMessage());
        return result;
    }
  
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Result handleException(RuntimeException e) {
        logger.error("异常信息:", e.getMessage());
        Result result = new Result();
        result.setStatus(500);
        result.setMessage(e.getMessage());
        return result;
    }
  
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public AjaxJson doBusinessException(Exception e) {
        AjaxJson ajaxJson = new AjaxJson();
        logger.error("业务异常消息:", e.getMessage());
        ajaxJson.setRet(-1);
        ajaxJson.setMsg(e.getMessage());
        return ajaxJson;
    }
  
}

测试代码

1.我们捕获一个业务异常BusinessException,输出aaa

 

2.捕获系统异常

1
throw new SystemException("aaaa");

3.其他的try catch的异常,这个就可以捕获了 

到此这篇关于springboot 自定义异常并捕获异常返给前端的文章就介绍到这了

原文链接:https://blog.csdn.net/ying456baby/article/details/121291964


文章分类
代码人生
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 gxwowoo@163.com 举报,一经查实,本站将立刻删除。
相关推荐