日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

全局异常捕获处理-@ControllerAdvice+@HandleException

發布時間:2025/5/22 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 全局异常捕获处理-@ControllerAdvice+@HandleException 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

涂涂影院管理系統這個demo中有個異常管理的標簽,用于捕獲 涂涂影院APP用戶異常信息 ,有小伙伴好奇,排除APP,后臺端的是如何處理全局異常的,故項目中的實際應用已記之。

關于目前的異常處理

在使用全局異常處理之前,就目前我們是如何處理程序中的異常信息的呢?

throws Exception + try-catch

怎么講?

在我們目前項目中,往往事務發生在 Service 層,因為會牽扯到調用 Dao 跟數據庫打交道,當數據庫操作失敗時,會讓 Service 層拋出運行時異常,Spring 事物管理器就會進行回滾。

Service 拋出異常,那么 Controller 必然要去捕獲,處理異常,所以,try-catch 就出現了。

看一下Service:

public?interface?ServiceI{
????##?保存實體的方法
????public?Serializable?save(Entity?entity)?throws?Exception;
}

看一下Controller的某個調用方法:

@PostMapping(value?=?"")
public?AppResponse?add(@RequestBody?Entity?entity,?Errors?errors){
????AppResponse?resp?=?new?AppResponse();
????try?{
????????Entity?endity?=?new?Entity();
????????endity.setXxx();

????????ServiceI.save(dog);

????????##?返回數據
????????resp.setData(newDog);

????}catch?(BusinessException?e){
????????resp.setFail(e.getMessage());
????}catch?(Exception?e){
????????resp.setFail("操作失敗!");
????}
????return?resp;
}

看上去也沒什么別就,但是一個類中出現大面積的 try-catch ,就顯得非常難看且冗余。

如果使用 @ControllerAdvice + @ExceptionHandler 進行全局的 Controller 層異常處理,只要設計得當,就再也不用在 Controller 層進行 try-catch 了。

書寫全局異常處理

1. @ControllerAdvice注解

定義全局異常處理類,@RestControllerAdvice 為 @ResponseBody + @ControllerAdvice

@Slf4j
@RestControllerAdvice
public?class?RestCtrlExceptionHandler?{

}
2. @ExceptionHandler注解

聲明異常處理方法,方法 handleException() 就會處理所有 Controller 層拋出的 Exception 及其子類的異常,這是最基本的用法了。

????@ExceptionHandler(Exception.class)
????@ResponseStatus(value?=?HttpStatus.OK)
????public?Result<Object>?handleException(Exception?e)?{

????????String?errorMsg?=?"Exception";
????????if?(e!=null){
????????????errorMsg?=?e.getMessage();
????????????log.error(e.toString());
????????}
????????return?new?ResultUtil<>().setErrorMsg(500,?errorMsg);
????}

結合上邊1、2組合一下:

@Slf4j
@RestControllerAdvice
public?class?RestCtrlExceptionHandler?{

????@ExceptionHandler(TmaxException.class)
????@ResponseStatus(value?=?HttpStatus.OK)
????public?Result<Object>?handleXCloudException(TmaxException?e)?{

????????String?errorMsg?=?"Tmax?exception";
????????if?(e!=null){
????????????errorMsg?=?e.getMsg();
????????????log.error(e.toString());
????????}
????????return?new?ResultUtil<>().setErrorMsg(500,?errorMsg);
????}

????@ExceptionHandler(Exception.class)
????@ResponseStatus(value?=?HttpStatus.OK)
????public?Result<Object>?handleException(Exception?e)?{

????????String?errorMsg?=?"Exception";
????????if?(e!=null){
????????????errorMsg?=?e.getMessage();
????????????log.error(e.toString());
????????}
????????return?new?ResultUtil<>().setErrorMsg(500,?errorMsg);
????}
}

看一下 handleXCloudException() 方法

通常我們需要拋出我們自定義異常,而不是一有異常就全部進入 handleException 中,該方法中 TmaxException 即為我們自定義的異常。

@Data
public?class?TmaxException?extends?RuntimeException?{

????private?String?msg;

????public?TmaxException(String?msg){
????????super(msg);
????????this.msg?=?msg;
????}
}

這樣,我們就可以在 Controller 中拋出我們定義的異常了,比如:

throw?new?TmaxException("連接ES失敗,請檢查ES運行狀態");

如果文章有錯的地方歡迎指正,大家互相留言交流。習慣在微信看技術文章,想要獲取更多的Java資源的同學,可以關注微信公眾號:niceyoo

轉載于:https://www.cnblogs.com/niceyoo/p/10913851.html

總結

以上是生活随笔為你收集整理的全局异常捕获处理-@ControllerAdvice+@HandleException的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。