springboot全局異常處理詳解
一、單個(gè)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("測(cè)試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; } }
說(shuō)明:
- 在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; } }
說(shuō)明:
- @ControllerAdvice是controller的一個(gè)輔助類,最常用的就是作為全局異常處理的切面類
- @ControllerAdvice可以指定掃描范圍
- @ControllerAdvice約定了幾種可行的返回值,如果是直接返回model類的話,需要使用@ResponseBody進(jìn)行json轉(zhuǎn)換
- 返回String,表示跳到某個(gè)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("測(cè)試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; // } }
注意:
- 同一個(gè)異常被局部范圍異常處理器和全局范圍異常處理器同時(shí)覆蓋,會(huì)選擇小范圍的局部范圍處理器
- 同一個(gè)異常被小范圍的異常類和大范圍的異常處理器同時(shí)覆蓋,會(huì)選擇小范圍的異常處理器
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合MyBatis和MyBatis-Plus請(qǐng)求后不打印sql日志的問(wèn)題解決
本文主要介紹了SpringBoot整合MyBatis和MyBatis-Plus請(qǐng)求后不打印sql日志的問(wèn)題解決文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07淺談Java并發(fā)中ReentrantLock鎖應(yīng)該怎么用
本文主要介紹了ava并發(fā)中ReentrantLock鎖的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Spring三種方法的注解自動(dòng)注入問(wèn)題
這篇文章主要介紹了Spring三種方法的注解自動(dòng)注入問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Java微信掃碼登錄功能并實(shí)現(xiàn)認(rèn)證授權(quán)全過(guò)程
這篇文章主要給大家介紹了關(guān)于Java微信掃碼登錄功能并實(shí)現(xiàn)認(rèn)證授權(quán)的相關(guān)資料,要在Java中實(shí)現(xiàn)微信掃碼登錄,您可以按照以下步驟進(jìn)行操作,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10idea如何debug看springsecurity的過(guò)濾器順序
這篇文章主要介紹了idea如何debug看springsecurity的過(guò)濾器順序,文中通過(guò)圖文結(jié)合的方式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04mybatis-plus的selectById(或者selectOne)在根據(jù)主鍵ID查詢實(shí)體對(duì)象的時(shí)候偶爾會(huì)出現(xiàn)nul
這篇文章主要介紹了mybatis-plus的selectById(或者selectOne)在根據(jù)主鍵ID查詢實(shí)體對(duì)象的時(shí)候偶爾會(huì)出現(xiàn)null的問(wèn)題記錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09基于Spring Mvc實(shí)現(xiàn)的Excel文件上傳下載示例
本篇文章主要介紹了基于Spring Mvc實(shí)現(xiàn)的Excel文件上傳下載示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02