Spring Boot全局统一异常处理器
程序员文章站
2022-07-03 14:54:47
一、封装统一返回结果类import com.jiusen.exercise.enums.errorenum;import com.jiusen.exercise.exception.businesse...
一、封装统一返回结果类
import com.jiusen.exercise.enums.errorenum; import com.jiusen.exercise.exception.businessexception; import lombok.getter; import lombok.setter; /** * @author: lawson * @date: 2021/5/11 * @description: todo 统一的返回结果 */ @getter @setter public class ajaxresult { //是否成功 private boolean success; //状态码 private integer code; //提示信息 private string msg; //数据 private object data; public ajaxresult() { } //自定义返回结果的构造方法 public ajaxresult(boolean success, integer code, string msg, object data) { this.success = success; this.code = code; this.msg = msg; this.data = data; } //自定义异常返回的结果 public static ajaxresult defineerror(businessexception de) { ajaxresult result = new ajaxresult(); result.setsuccess(false); result.setcode(de.geterrorcode()); result.setmsg(de.geterrormsg()); result.setdata(null); return result; } //其他异常处理方法返回的结果 public static ajaxresult othererror(errorenum errorenum){ ajaxresult result = new ajaxresult(); result.setmsg(errorenum.geterrormsg()); result.setcode(errorenum.geterrorcode()); result.setsuccess(false); result.setdata(null); return result; } }
二、自定义异常封装类
import lombok.getter; import lombok.setter; /** * @author: lawson * @date: 2021/5/11 * @description: todo 类描述 */ @getter @setter public class businessexception extends runtimeexception{ private static final long serialversionuid = 1l; /** * 错误状态码 */ protected integer errorcode; /** * 错误提示 */ protected string errormsg; public businessexception() { } public businessexception(string message, integer errorcode, string errormsg) { super(message); this.errorcode = errorcode; this.errormsg = errormsg; } }
三、错误枚举
** * @author: lawson * @date: 2021/5/11 * @description: todo 类描述 */ public enum errorenum { //数据操作错误定义 success(200, "成功"), no_permission(403, "你无权访问"), no_auth(401, "未授权,请登录验证"), no_found(404, "未找到资源"), internal_server_error(500, "服务器异常, 请联系管理员!"); /** * 错误码 */ private integer errorcode; /** * 错误信息 */ private string errormsg; errorenum(integer errorcode, string errormsg) { this.errorcode = errorcode; this.errormsg = errormsg; } public integer geterrorcode() { return errorcode; } public string geterrormsg() { return errormsg; } }
四、全局异常处理类
import com.jiusen.exercise.enums.errorenum; import com.jiusen.exercise.exception.businessexception; import com.jiusen.exercise.rest.ajaxresult; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.web.bind.annotation.exceptionhandler; import org.springframework.web.bind.annotation.restcontrolleradvice; /** * @author: lawson * @date: 2021/5/11 * @description: todo 类描述 */ @restcontrolleradvice public class globalexceptionhandler { private static final logger log = loggerfactory.getlogger(globalexceptionhandler.class); /** * 处理自定义异常 * @param e * @return */ @exceptionhandler(value = businessexception.class) public ajaxresult bizexceptionhandler(businessexception e) { log.error(e.geterrormsg(), e); return ajaxresult.defineerror(e); } /** * 处理其它异常 */ @exceptionhandler(value = exception.class) public ajaxresult exceptionhandler(exception e) { log.error(e.getmessage(), e); return ajaxresult.othererror(errorenum.internal_server_error); } }
五、测试
import com.jiusen.exercise.enums.errorenum; import com.jiusen.exercise.exception.businessexception; import com.jiusen.exercise.rest.ajaxresult; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.web.bind.annotation.exceptionhandler; import org.springframework.web.bind.annotation.restcontrolleradvice; /** * @author: lawson * @date: 2021/5/11 * @description: todo 类描述 */ @restcontrolleradvice public class globalexceptionhandler { private static final logger log = loggerfactory.getlogger(globalexceptionhandler.class); /** * 处理自定义异常 * @param e * @return */ @exceptionhandler(value = businessexception.class) public ajaxresult bizexceptionhandler(businessexception e) { log.error(e.geterrormsg(), e); return ajaxresult.defineerror(e); } /** * 处理其它异常 */ @exceptionhandler(value = exception.class) public ajaxresult exceptionhandler(exception e) { log.error(e.getmessage(), e); return ajaxresult.othererror(errorenum.internal_server_error); } }
到此这篇关于spring boot全局统一异常处理器的文章就介绍到这了,更多相关spring boot异常处理器内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!