欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-09 19:23:45
背景在开发中,如果用try catch的方式,每个方法都需要单独实现,为了方便分类异常,返回给前端,采用了@controlleradvice注解和继承了runtimeexception的方式来实现。实...

背景

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

实现内容

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

          businessexception

2.系统异常 

        systemexception

3.其他异常

        利用@exceptionhandler(runtimeexception.class)去捕获

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

实现代码

业务异常businessexception类实现方式,继承runtimeexception

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,同业务异常类的实现方式一样

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,就是捕获的异常类型,我们只需要把需要捕获异常类型写进来就好

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

 exceptionadvice 具体代码实现如下:

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

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

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

2.捕获系统异常

throw new systemexception("aaaa");

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

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

到此这篇关于springboot 自定义异常并捕获异常返给前端的文章就介绍到这了,更多相关springboot 自定义异常内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!