SpringBoot中使用@ControllerAdvice注解詳解
@ControllerAdvice注解
@ControllerAdvice,是Spring3.2提供的新注解,它是一個(gè)Controller增強(qiáng)器,可對(duì)controller中被 @RequestMapping注解的方法加一些邏輯處理。主要作用有一下三種
- 通過@ControllerAdvice注解可以將對(duì)于控制器的全局配置放在同一個(gè)位置。
- 注解了@ControllerAdvice的類的方法可以使用@ExceptionHandler、@InitBinder、@ModelAttribute注解到方法上。
- @ExceptionHandler:用于全局處理控制器里的異常,進(jìn)行全局異常處理
- @InitBinder:用來設(shè)置WebDataBinder,用于自動(dòng)綁定前臺(tái)請(qǐng)求參數(shù)到Model中,全局?jǐn)?shù)據(jù)預(yù)處理。
- @ModelAttribute:本來作用是綁定鍵值對(duì)到Model中,此處讓全局的@RequestMapping都能獲得在此處設(shè)置的鍵值對(duì) ,全局?jǐn)?shù)據(jù)綁定。
- @ControllerAdvice注解將作用在所有注解了@RequestMapping的控制器的方法上。
全局異常處理
需要配合@ExceptionHandler使用。 當(dāng)將異常拋到controller時(shí),可以對(duì)異常進(jìn)行統(tǒng)一處理,規(guī)定返回的json格式或是跳轉(zhuǎn)到一個(gè)錯(cuò)誤頁面
/** * @ClassName:CustomExceptionHandler * @Description: 全局異常捕獲 * @Author: * @Date: 2020/5/25、13:38 */ @Slf4j @ControllerAdvice(annotations = {Controller.class, RestController.class}) public class WebControllerAdvice { @ResponseBody @ExceptionHandler public Map errorHandler(Exception ex) { Map errorMap = new HashMap(); errorMap.put("code", 400); //判斷異常的類型,返回不一樣的返回值 if (ex instanceof MissingServletRequestParameterException) { errorMap.put("msg", "缺少必需參數(shù):" + ((MissingServletRequestParameterException) ex).getParameterName()); } else if (ex instanceof MyException) { errorMap.put("msg", "這是自定義異常"); } return errorMap; }
自定義異常
/** * @ClassName:MyException * @Description: 定義異常 * @Author: * @Date: 2020/5/25、13:44 */ public class MyException extends RuntimeException { private long code; private String msg; public MyException(Long code, String msg) { super(msg); this.code = code; this.msg = msg; } public MyException(String msg) { super(msg); this.msg = msg; } }
測(cè)試Controller
@RestController public class TestController { @RequestMapping("testException") public String testException() throws Exception{ throw new MissingServletRequestParameterException("name","String"); } @RequestMapping("testMyException") public String testMyException() throws MyException{ throw new MyException("i am a myException"); }
測(cè)試結(jié)果:
{"msg":"缺少必需參數(shù):name","code":400}
{"msg":"這是自定義異常","code":400}
全局?jǐn)?shù)據(jù)綁定
全局?jǐn)?shù)據(jù)綁定功能可以用來做一些初始化的數(shù)據(jù)操作,我們可以將一些公共的數(shù)據(jù)定義在添加了 @ControllerAdvice 注解的類中,這樣,在每一個(gè) Controller 的接口中,就都能夠訪問導(dǎo)致這些數(shù)據(jù)。使用步驟,首先定義全局?jǐn)?shù)據(jù),如下:
/** * @ClassName:MyGlobalDataHandler * @Description: 全局?jǐn)?shù)據(jù) * @Author: * @Date: 2020/5/25、14:01 */ @ControllerAdvice public class MyGlobalDataHandler { @ModelAttribute(name = "md") public Map<String,Object> getGlobalData(){ HashMap<String, Object> map = new HashMap<>(); map.put("age", 99); map.put("gender", "男"); return map; }
使用 @ModelAttribute 注解標(biāo)記該方法的返回?cái)?shù)據(jù)是一個(gè)全局?jǐn)?shù)據(jù),默認(rèn)情況下,這個(gè)全局?jǐn)?shù)據(jù)的 key 就是返回的變量名,value 就是方法返回值,當(dāng)然開發(fā)者可以通過 @ModelAttribute 注解的 name 屬性去重新指定 key。定義完成后,在任何一個(gè)Controller 的接口中,都可以獲取到這里定義的數(shù)據(jù):
@GetMapping("/hello") public String hello(Model model) { Map<String, Object> map = model.asMap(); System.out.println(map); int i = 1 / 0; return "hello controller advice"; }
運(yùn)行結(jié)果
{md={gender=男, age=99}}
2020-05-25 14:04:44.388 - [WARN ] - [org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver:logException:197] - Resolved [java.lang.ArithmeticException: / by zero]
全局?jǐn)?shù)據(jù)預(yù)處理
考慮我有兩個(gè)實(shí)體類,Book 和 Author,分別定義如下:
@Getter @Setter @AllArgsConstructor @NoArgsConstructor public class Book extends BaseEntity { private String name; private Long price; } @Getter @Setter @AllArgsConstructor @NoArgsConstructor public class Author extends BaseEntity { private String name; private Long price; }
如果我定義一個(gè)數(shù)據(jù)添加接口,如下:
@PostMapping("/book") public void addBook(Book book, Author author) { System.out.println(book); System.out.println(author); }
這個(gè)時(shí)候,添加操作就會(huì)有問題,因?yàn)閮蓚€(gè)實(shí)體類都有一個(gè) name 屬性,從前端傳遞時(shí) ,無法區(qū)分。此時(shí),通過 @ControllerAdvice 的全局?jǐn)?shù)據(jù)預(yù)處理可以解決這個(gè)問題
解決步驟如下:
1.給接口中的變量取別名
@PostMapping("/book") public void addBook(@ModelAttribute("b") Book book, @ModelAttribute("a") Author author) { System.out.println(book); System.out.println(author); }
2.進(jìn)行請(qǐng)求數(shù)據(jù)預(yù)處理
在 @ControllerAdvice 標(biāo)記的類中添加如下代碼:
@InitBinder("b") public void b(WebDataBinder binder) { binder.setFieldDefaultPrefix("b."); } @InitBinder("a") public void a(WebDataBinder binder) { binder.setFieldDefaultPrefix("a."); }
@InitBinder("b") 注解表示該方法用來處理和Book和相關(guān)的參數(shù),在方法中,給參數(shù)添加一個(gè) b 前綴,即請(qǐng)求參數(shù)要有b前綴
3.發(fā)送請(qǐng)求
請(qǐng)求發(fā)送時(shí),通過給不同對(duì)象的參數(shù)添加不同的前綴,可以實(shí)現(xiàn)參數(shù)的區(qū)分
到此這篇關(guān)于SpringBoot中使用@ControllerAdvice注解詳解的文章就介紹到這了,更多相關(guān)SpringBoot的@ControllerAdvice注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot Application事件監(jiān)聽的實(shí)現(xiàn)方案
這篇文章主要介紹了SpringBoot Application事件監(jiān)聽的實(shí)現(xiàn)方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Java?IO篇之Reactor?網(wǎng)絡(luò)模型的概念
Reactor?模式也叫做反應(yīng)器設(shè)計(jì)模式,是一種為處理服務(wù)請(qǐng)求并發(fā)提交到一個(gè)或者多個(gè)服務(wù)處理器的事件設(shè)計(jì)模式,Reactor?模式主要由?Reactor?和處理器?Handler?這兩個(gè)核心部分組成,本文給大家介紹Java?IO篇之Reactor?網(wǎng)絡(luò)模型的概念,感興趣的朋友一起看看吧2022-01-01Java實(shí)現(xiàn)Huffman編碼的示例代碼
Huffman編碼是一種編碼方式,本文主要介紹了Java實(shí)現(xiàn)Huffman編碼的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08java?MongoDB實(shí)現(xiàn)列表分頁查詢的示例代碼
本文主要介紹了java?MongoDB實(shí)現(xiàn)列表分頁查詢的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07解決IDEA?JDK9沒有module-info.java的問題
這篇文章主要介紹了解決IDEA?JDK9沒有module-info.java的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01