背景
在项目中我们有需求做一个全局异常处理,来规范所有出去的异常信息。
参考:官方文档
分析
首先 controlleradvice(restcontrolleradvice ) ,controlleradvice 是无法处理过滤器和拦截器中的异常的。
引用一张图
下面介绍controller层的全局异常设置
全局异常处理也有多种方式
使用@controlleradvice(@restcontrolleradvice)+@exceptionhandler实现全局异常
import lombok.extern.slf4j.slf4j; import org.springframework.http.httpstatus; import org.springframework.web.bind.annotation.controlleradvice; import org.springframework.web.bind.annotation.exceptionhandler; import org.springframework.web.bind.annotation.responsebody; @slf4j @controlleradvice public class globalexceptionhandler { /** * 处理参数错误的异常 * @param e * @return */ @responsebody @exceptionhandler(value = illegalparamsexception.class) public resultvo<object> handleillegalparamsexception(illegalparamsexception e) { resultvo<object> resultvo = new resultvo<>(); resultvo.setstatus(httpstatus.bad_request.value()); resultvo.seterrorcode(e.geterrorinfo().geterrorcode()); resultvo.seterrormsg(e.geterrorinfo().geterrordesc()); return resultvo; } @responsebody @exceptionhandler(value = exception.class) public resultvo<object> handleexception(exception e) { resultvo<object> resultvo = new resultvo<>(); resultvo.setstatus(httpstatus.internal_server_error.value()); resultvo.seterrormsg(e.getmessage()); return resultvo; } }
@data @allargsconstructor @noargsconstructor public class resultvo<t> { private integer status; private string errorcode; private string errormsg; private t data; public resultvo(integer status, string errorcode, string errormsg) { this.status = status; this.errorcode = errorcode; this.errormsg = errormsg; } }
public class illegalparamsexception extends runtimeexception { private static final long serialversionuid = -6298406656682893468l; private operationerrorenum errorinfo; public illegalparamsexception(operationerrorenum errorinfo) { this.errorinfo = errorinfo; } public illegalparamsexception(string message, operationerrorenum errorinfo) { super(message); this.errorinfo = errorinfo; } public illegalparamsexception(string message, throwable cause, operationerrorenum errorinfo) { super(message, cause); this.errorinfo = errorinfo; } public illegalparamsexception(throwable cause, operationerrorenum errorinfo) { super(cause); this.errorinfo = errorinfo; } public illegalparamsexception(string message, throwable cause, boolean enablesuppression, boolean writablestacktrace, operationerrorenum errorinfo) { super(message, cause, enablesuppression, writablestacktrace); this.errorinfo = errorinfo; } public operationerrorenum geterrorinfo() { return errorinfo; } }
全局异常处理-多个处理器匹配顺序
参考:
多个处理器的两种情况:
存在一个类中
子类异常处理器优先
存在不同的类中
与多个异常处理类放入linkedhashmap的顺序有关,
可以利用order指定顺序,如果没有,则默认最小顺序;
那么,如果都没有指定顺序的话,那就是list中的顺序
对于过滤器和拦截器中的异常,有两种思路可以考虑
1、catch后通过转发到异常页面(设置modelandview)
参考:
2、拦截器中发生异常,拦截器中直接返回错误(通过response.getoutputstream().write() 直接写错误信息)
如:
@override public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception { try { // 业务代码 } catch (exception e) { response.setcontenttype(mediatype.application_json_utf8_value); resultvo<object> resultvo = new resultvo<>(); resultvo.setstatus(httpstatus.unauthorized.value()); resultvo.seterrormsg(access_param_error.geterrordesc()); response.getoutputstream().write(new string(json.tojsonstring(resultvo)).getbytes(standardcharsets.utf_8)); logger.error("==== whitelistandauthenticationinterceptor拦截器拦截到了方法:{} 解析鉴权参数异常 ====", methodname); return false; } }
到此这篇关于springmvc设置全局异常处理器的步骤的文章就介绍到这了,更多相关springmvc全局异常处理器内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论