javascript
Spring的@ExceptionHandler注解使用方法
文章目錄
- 1,基本使用方法
- 2,注解的參數
- 3,就近原則
- 4,注解方法的返回值
- 5,錯誤的操作
1,基本使用方法
Spring的@ExceptionHandler可以用來統一處理方法拋出的異常,比如這樣:
@ExceptionHandler() public String handleExeption2(Exception ex) {System.out.println("拋異常了:" + ex);ex.printStackTrace();String resultStr = "異常:默認";return resultStr; }當我們使用這個@ExceptionHandler注解時,我們需要定義一個異常的處理方法,比如上面的handleExeption2()方法,給這個方法加上@ExceptionHandler注解,這個方法就會處理類中其他方法(被@RequestMapping注解)拋出的異常。
2,注解的參數
@ExceptionHandler注解中可以添加參數,參數是某個異常類的class,代表這個方法專門處理該類異常,比如這樣:
@ExceptionHandler(NumberFormatException.class) public String handleExeption(Exception ex) {System.out.println("拋異常了:" + ex);ex.printStackTrace();String resultStr = "異常:NumberFormatException";return resultStr; }此時注解的參數是NumberFormatException.class,表示只有方法拋出NumberFormatException時,才會調用該方法。
3,就近原則
當異常發生時,Spring會選擇最接近拋出異常的處理方法。
比如之前提到的NumberFormatException,這個異常有父類RuntimeException,RuntimeException還有父類Exception,如果我們分別定義異常處理方法,@ExceptionHandler分別使用這三個異常作為參數,比如這樣:
@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時,調用的方法將是注解參數NumberFormatException.class的方法,也就是handleExeption(),而當代碼拋出IndexOutOfBoundsException時,調用的方法將是注解參數RuntimeException的方法,也就是handleExeption3()。
4,注解方法的返回值
標識了@ExceptionHandler注解的方法,返回值類型和標識了@RequestMapping的方法是統一的,可參見@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; }這樣的操作可以在執行完方法后直接返回字符串本身。
5,錯誤的操作
使用@ExceptionHandler時盡量不要使用相同的注解參數。
如果我們定義兩個處理相同異常的處理方法:
@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] 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Spring的@ExceptionHandler注解使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【直播预告】第四范式Intel AI应用
- 下一篇: Log4j2突发重大漏洞