springboot全局異常處理詳解
一、單個controller范圍的異常處理
package com.xxx.secondboot.web; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.xxx.secondboot.exception.MyExceptionResponse; import io.swagger.annotations.Api; @Api("測試controllerAdvice和全局異常處理") @RestController @RequestMapping("/advice1") public class AdviceController { @RequestMapping(value = "/test1", method = RequestMethod.GET) public String test1() { throw new RuntimeException("advice1 - exception1"); } @RequestMapping(value = "/test2", method = RequestMethod.GET) public String test2() { throw new RuntimeException("advice1 - exception2"); } @ExceptionHandler(RuntimeException.class) public MyExceptionResponse exceptionHandler() { MyExceptionResponse resp = new MyExceptionResponse(); resp.setCode(300); resp.setMsg("exception-Handler"); return resp; } }
說明:
- 在controller中加入被@ExceptionHandler修飾的類即可(在該注解中指定該方法需要處理的那些異常類)
- 該異常處理方法只在當(dāng)前的controller中起作用
二、全部controller范圍內(nèi)起作用的異常處理(全局異常處理)
1、全局異常處理類
package com.xxx.secondboot.web; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.xxx.secondboot.exception.MyExceptionResponse; import com.xxx.secondboot.exception.MyRuntimeException; //@ControllerAdvice(annotations=RestController.class) //@ControllerAdvice(basePackages={"com.xxx","com.ooo"}) @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(RuntimeException.class) // @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class}) // @ExceptionHandler//處理所有異常 @ResponseBody //在返回自定義相應(yīng)類的情況下必須有,這是@ControllerAdvice注解的規(guī)定 public MyExceptionResponse exceptionHandler(RuntimeException e, HttpServletResponse response) { MyExceptionResponse resp = new MyExceptionResponse(); resp.setCode(300); resp.setMsg("exception-Handler"); // response.setStatus(600); return resp; } }
說明:
- @ControllerAdvice是controller的一個輔助類,最常用的就是作為全局異常處理的切面類
- @ControllerAdvice可以指定掃描范圍
- @ControllerAdvice約定了幾種可行的返回值,如果是直接返回model類的話,需要使用@ResponseBody進(jìn)行json轉(zhuǎn)換
- 返回String,表示跳到某個view
- 返回modelAndView
- 返回model + @ResponseBody
2、controller
package com.xxx.secondboot.web; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; @Api("測試controllerAdvice和全局異常處理") @RestController @RequestMapping("/advice1") public class AdviceController { @RequestMapping(value = "/test1", method = RequestMethod.GET) public String test1() { throw new RuntimeException("advice1 - exception1"); } @RequestMapping(value = "/test2", method = RequestMethod.GET) public String test2() { throw new RuntimeException("advice1 - exception2"); } // @ExceptionHandler(RuntimeException.class) // public MyExceptionResponse exceptionHandler() { // MyExceptionResponse resp = new MyExceptionResponse(); // resp.setCode(300); // resp.setMsg("exception-Handler"); // return resp; // } }
注意:
- 同一個異常被局部范圍異常處理器和全局范圍異常處理器同時覆蓋,會選擇小范圍的局部范圍處理器
- 同一個異常被小范圍的異常類和大范圍的異常處理器同時覆蓋,會選擇小范圍的異常處理器
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合MyBatis和MyBatis-Plus請求后不打印sql日志的問題解決
本文主要介紹了SpringBoot整合MyBatis和MyBatis-Plus請求后不打印sql日志的問題解決文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07淺談Java并發(fā)中ReentrantLock鎖應(yīng)該怎么用
本文主要介紹了ava并發(fā)中ReentrantLock鎖的具體使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11Java微信掃碼登錄功能并實現(xiàn)認(rèn)證授權(quán)全過程
這篇文章主要給大家介紹了關(guān)于Java微信掃碼登錄功能并實現(xiàn)認(rèn)證授權(quán)的相關(guān)資料,要在Java中實現(xiàn)微信掃碼登錄,您可以按照以下步驟進(jìn)行操作,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10idea如何debug看springsecurity的過濾器順序
這篇文章主要介紹了idea如何debug看springsecurity的過濾器順序,文中通過圖文結(jié)合的方式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04mybatis-plus的selectById(或者selectOne)在根據(jù)主鍵ID查詢實體對象的時候偶爾會出現(xiàn)nul
這篇文章主要介紹了mybatis-plus的selectById(或者selectOne)在根據(jù)主鍵ID查詢實體對象的時候偶爾會出現(xiàn)null的問題記錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09基于Spring Mvc實現(xiàn)的Excel文件上傳下載示例
本篇文章主要介紹了基于Spring Mvc實現(xiàn)的Excel文件上傳下載示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02