Spring中的@ExceptionHandler異常攔截器
@ExceptionHandler異常攔截器
1,基本使用方法
Spring的@ExceptionHandler可以用來統(tǒng)一處理方法拋出的異常,比如這樣:
@ExceptionHandler() public String handleExeption2(Exception ex) { System.out.println("拋異常了:" + ex); ex.printStackTrace(); String resultStr = "異常:默認"; return resultStr; }
當我們使用這個@ExceptionHandler注解時,我們需要定義一個異常的處理方法,比如上面的handleExeption2()方法,給這個方法加上@ExceptionHandler注解,這個方法就會處理類中其他方法(被@RequestMapping注解)拋出的異常。
2,注解的參數(shù)
@ExceptionHandler注解中可以添加參數(shù),參數(shù)是某個異常類的class,代表這個方法專門處理該類異常,比如這樣:
@ExceptionHandler(NumberFormatException.class) public String handleExeption(Exception ex) { System.out.println("拋異常了:" + ex); ex.printStackTrace(); String resultStr = "異常:NumberFormatException"; return resultStr; }
此時注解的參數(shù)是NumberFormatException.class,表示只有方法拋出NumberFormatException時,才會調(diào)用該方法。
3,就近原則
當異常發(fā)生時,Spring會選擇最接近拋出異常的處理方法。
比如之前提到的NumberFormatException,這個異常有父類RuntimeException,RuntimeException還有父類Exception,如果我們分別定義異常處理方法,@ExceptionHandler分別使用這三個異常作為參數(shù),比如這樣:
@ExceptionHandler(NumberFormatException.class) public String handleExeption(Exception ex) { System.out.println("拋異常了:" + ex); ex.printStackTrace(); String resultStr = "異常:NumberFormatException"; return resultStr; } @ExceptionHandler() public String handleExeption2(Exception ex) { System.out.println("拋異常了:" + ex); ex.printStackTrace(); String resultStr = "異常:默認"; return resultStr; } @ExceptionHandler(RuntimeException.class) public String handleExeption3(Exception ex) { System.out.println("拋異常了:" + ex); ex.printStackTrace(); String resultStr = "異常:RuntimeException"; return resultStr; }
那么,當代碼拋出NumberFormatException時,調(diào)用的方法將是注解參數(shù)NumberFormatException.class的方法,也就是handleExeption(),而當代碼拋出IndexOutOfBoundsException時,調(diào)用的方法將是注解參數(shù)RuntimeException的方法,也就是handleExeption3()。
4,注解方法的返回值
標識了@ExceptionHandler注解的方法,返回值類型和標識了@RequestMapping的方法是統(tǒng)一的,可參見@RequestMapping的說明,比如默認返回Spring的ModelAndView對象,也可以返回String,這時的String是ModelAndView的路徑,而不是字符串本身。
有些情況下我們會給標識了@RequestMapping的方法添加@ResponseBody,比如使用Ajax的場景,直接返回字符串,異常處理類也可以如此操作,添加@ResponseBody注解后,可以直接返回字符串,比如這樣:
@ExceptionHandler(NumberFormatException.class) @ResponseBody public String handleExeption(Exception ex) { System.out.println("拋異常了:" + ex); ex.printStackTrace(); String resultStr = "異常:NumberFormatException"; return resultStr; }
這樣的操作可以在執(zhí)行完方法后直接返回字符串本身。
5,錯誤的操作
使用@ExceptionHandler時盡量不要使用相同的注解參數(shù)。
如果我們定義兩個處理相同異常的處理方法:
@ExceptionHandler(NumberFormatException.class) @ResponseBody public String handleExeption(Exception ex) { System.out.println("拋異常了:" + ex); ex.printStackTrace(); String resultStr = "異常:NumberFormatException"; return resultStr; } @ExceptionHandler(NumberFormatException.class) @ResponseBody public String handleExeption2(Exception ex) { System.out.println("拋異常了:" + ex); ex.printStackTrace(); String resultStr = "異常:默認"; return resultStr; }
兩個方法都處理NumberFormatException,這種定義方式編譯可以通過,而當NumberFormatException真正被拋出時,Spring會給我們報錯:
java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class java.lang.NumberFormatException]: {public java.lang.String TestController.handleExeption(java.lang.Exception), public java.lang.String TestController.handleExeption2(java.lang.Exception)}
at org.springframework.web.method.annotation.ExceptionHandlerMethodResolver.addExceptionMapping(ExceptionHandlerMethodResolver.java:102) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.method.annotation.ExceptionHandlerMethodResolver.<init>(ExceptionHandlerMethodResolver.java:66) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.getExceptionHandlerMethod(ExceptionHandlerExceptionResolver.java:458) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:378) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:61) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:140) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:78) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1255) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1062) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1008) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) ~[javax.servlet-api-3.1.0.jar:3.1.0]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) ~[javax.servlet-api-3.1.0.jar:3.1.0]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.29.jar:8.5.29]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) [tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.29.jar:8.5.29]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.29.jar:8.5.29]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_172]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_172]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.29.jar:8.5.29]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_172]
到此這篇關于Spring中的@ExceptionHandler異常攔截器的文章就介紹到這了,更多相關@ExceptionHandler異常攔截器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Springboot定時任務Scheduled重復執(zhí)行操作
這篇文章主要介紹了Springboot定時任務Scheduled重復執(zhí)行操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09深入了解Java核心類庫--BigDecimal和System類
這篇文章主要為大家詳細介紹了javaBigDecimal和System類定義與使用的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能給你帶來幫助2021-07-07