Spring中@ExceptionHandler注解的使用方式
1:@ExceptionHandler介紹
@ExceptionHandler注解我們一般是用來自定義異常的。
可以認(rèn)為它是一個(gè)異常攔截器(處理器)。
異常間的層次關(guān)系

2: @ExceptionHandler的使用
極簡(jiǎn)測(cè)試,一共4個(gè)類
1、一個(gè)SpringBoot啟動(dòng)類
2、一個(gè)控制層
3、一個(gè)異常處理類
4、一個(gè)service類
啟動(dòng)類:ExceptionhandlerdemoApplication
package com.example.exceptionhandlerdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ExceptionhandlerdemoApplication {
public static void main(String[] args) {
SpringApplication.run(ExceptionhandlerdemoApplication.class, args);
}
}異常處理類
package com.example.exceptionhandlerdemo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
private final Logger logger = LogManager.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler({Exception.class}) //申明捕獲那個(gè)異常類
public String ExceptionDemo(Exception e) {
logger.error(e.getMessage(), e);
return "自定義異常返回";
}
}控制層TestControll
package com.example.exceptionhandlerdemo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/yu")
public class TestControll {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private UserInfoSerimpl userInfoSerimpl;
@ResponseBody
@RequestMapping("/test")
public String test(){
logger.info("11111111111");
userInfoSerimpl.saveUserInfo();
logger.info("2222222222");
return "sdfsfs";
}
}業(yè)務(wù)層:UserInfoSerimpl
package com.example.exceptionhandlerdemo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service("userInfoService")
public class UserInfoSerimpl {
private Logger logger = LoggerFactory.getLogger(UserInfoSerimpl.class);
public void saveUserInfo() {
logger.error("獲取用戶信息失敗");
test1();
logger.info("ddddddddd");
}
private void test1(){
logger.error("test1 失敗");
throw new RuntimeException();
}
}測(cè)試:http://localhost:8080/yu/test
輸出:自定義異常返回
關(guān)于ExceptionHandler定義的攔截器之間的優(yōu)先級(jí)
在GlobalExceptionHandler類中定義兩個(gè)攔截器
@ExceptionHandler({RuntimeException.class}) //申明捕獲那個(gè)異常類
public String RuntimeExceptionDemo(Exception e) {
logger.error(e.getMessage(), e);
return "運(yùn)行時(shí)異常返回";
}
@ExceptionHandler({NumberFormatException.class}) //申明捕獲那個(gè)異常類
public String NumberFormatExceptionDemo(Exception e) {
logger.error(e.getMessage(), e);
return "數(shù)字轉(zhuǎn)換異常返回";
}在UserInfoSerimpl的test1方法中定義一個(gè)數(shù)字轉(zhuǎn)換異常, 這個(gè)異常在運(yùn)行時(shí)異常之前出現(xiàn)。
private void test1(){
logger.error("test1 失敗");
String a = "123a";
Integer b = Integer.valueOf(a);
throw new RuntimeException();
}測(cè)試:http://localhost:8080/yu/test
輸出:自定義異常返回
結(jié)論:自定義的異常越詳細(xì),得到的異常結(jié)果就越詳細(xì)。
為什么不直接使用一個(gè)Exception完事
1:Exception什么的異常太過廣泛,我們直接拋出所有異常信息,對(duì)用戶而言是非常不友好的。
2:在事務(wù)管理里,如果我們自定義的異常繼承的是Exception, 則事務(wù)無(wú)效。如果我們是繼承RuntimeException,則不會(huì) 出現(xiàn)這個(gè)問題。
到此這篇關(guān)于Spring中@ExceptionHandler注解的使用方式的文章就介紹到這了,更多相關(guān)@ExceptionHandler注解的使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring中的@ExceptionHandler注解統(tǒng)一異常處理詳解
- SpringMVC使用@ExceptionHandler注解在Controller中處理異常
- Spring的異常處理@ExceptionHandler注解解析
- 關(guān)于SpringBoot使用@ExceptionHandler注解局部異常處理
- Spring中@ExceptionHandler注解的工作原理詳解
- Spring @ExceptionHandler注解統(tǒng)一異常處理和獲取方法名
- Spring中的@ControllerAdvice和@ExceptionHandler注解處理全局異常
- Spring中的@ExceptionHandler注解詳解與應(yīng)用示例
相關(guān)文章
java中對(duì)象轉(zhuǎn)json字符串的幾種常用方式舉例
這篇文章主要給大家介紹了關(guān)于java中對(duì)象轉(zhuǎn)json字符串的幾種常用方式,在Java中可以使用許多庫(kù)將對(duì)象轉(zhuǎn)換為JSON字符串,其中最常用的是Jackson和Gson,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
關(guān)于Springboot在新增和修改下上傳圖片并顯示的問題
這篇文章主要介紹了關(guān)于Springboot在新增和修改下上傳圖片并顯示的問題及解決方法,在這里 springboot中已經(jīng)內(nèi)嵌了上傳圖片的依賴包,因此不需要再添加額外依賴,具體實(shí)現(xiàn)代碼跟隨小編一起看看吧2021-04-04
SMBMS超市訂單管理系統(tǒng)的網(wǎng)站源碼
這篇文章主要介紹了SMBMS超市訂單管理系統(tǒng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
slf4j?jcl?jul?log4j1?log4j2?logback各組件系統(tǒng)日志切換
這篇文章主要介紹了slf4j、jcl、jul、log4j1、log4j2、logback的大總結(jié),各個(gè)組件的jar包以及目前系統(tǒng)日志需要切換實(shí)現(xiàn)方式的方法,有需要的朋友可以借鑒參考下2022-03-03

