SpringBoot中使用@ControllerAdvice注解詳解
@ControllerAdvice注解
@ControllerAdvice,是Spring3.2提供的新注解,它是一個Controller增強器,可對controller中被 @RequestMapping注解的方法加一些邏輯處理。主要作用有一下三種
- 通過@ControllerAdvice注解可以將對于控制器的全局配置放在同一個位置。
- 注解了@ControllerAdvice的類的方法可以使用@ExceptionHandler、@InitBinder、@ModelAttribute注解到方法上。
- @ExceptionHandler:用于全局處理控制器里的異常,進行全局異常處理
- @InitBinder:用來設置WebDataBinder,用于自動綁定前臺請求參數(shù)到Model中,全局數(shù)據(jù)預處理。
- @ModelAttribute:本來作用是綁定鍵值對到Model中,此處讓全局的@RequestMapping都能獲得在此處設置的鍵值對 ,全局數(shù)據(jù)綁定。
- @ControllerAdvice注解將作用在所有注解了@RequestMapping的控制器的方法上。
全局異常處理
需要配合@ExceptionHandler使用。 當將異常拋到controller時,可以對異常進行統(tǒng)一處理,規(guī)定返回的json格式或是跳轉(zhuǎn)到一個錯誤頁面
/** * @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; } }
測試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"); }
測試結(jié)果:
{"msg":"缺少必需參數(shù):name","code":400}
{"msg":"這是自定義異常","code":400}
全局數(shù)據(jù)綁定
全局數(shù)據(jù)綁定功能可以用來做一些初始化的數(shù)據(jù)操作,我們可以將一些公共的數(shù)據(jù)定義在添加了 @ControllerAdvice 注解的類中,這樣,在每一個 Controller 的接口中,就都能夠訪問導致這些數(shù)據(jù)。使用步驟,首先定義全局數(shù)據(jù),如下:
/** * @ClassName:MyGlobalDataHandler * @Description: 全局數(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 注解標記該方法的返回數(shù)據(jù)是一個全局數(shù)據(jù),默認情況下,這個全局數(shù)據(jù)的 key 就是返回的變量名,value 就是方法返回值,當然開發(fā)者可以通過 @ModelAttribute 注解的 name 屬性去重新指定 key。定義完成后,在任何一個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"; }
運行結(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]
全局數(shù)據(jù)預處理
考慮我有兩個實體類,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; }
如果我定義一個數(shù)據(jù)添加接口,如下:
@PostMapping("/book") public void addBook(Book book, Author author) { System.out.println(book); System.out.println(author); }
這個時候,添加操作就會有問題,因為兩個實體類都有一個 name 屬性,從前端傳遞時 ,無法區(qū)分。此時,通過 @ControllerAdvice 的全局數(shù)據(jù)預處理可以解決這個問題
解決步驟如下:
1.給接口中的變量取別名
@PostMapping("/book") public void addBook(@ModelAttribute("b") Book book, @ModelAttribute("a") Author author) { System.out.println(book); System.out.println(author); }
2.進行請求數(shù)據(jù)預處理
在 @ControllerAdvice 標記的類中添加如下代碼:
@InitBinder("b") public void b(WebDataBinder binder) { binder.setFieldDefaultPrefix("b."); } @InitBinder("a") public void a(WebDataBinder binder) { binder.setFieldDefaultPrefix("a."); }
@InitBinder("b") 注解表示該方法用來處理和Book和相關的參數(shù),在方法中,給參數(shù)添加一個 b 前綴,即請求參數(shù)要有b前綴
3.發(fā)送請求
請求發(fā)送時,通過給不同對象的參數(shù)添加不同的前綴,可以實現(xiàn)參數(shù)的區(qū)分
到此這篇關于SpringBoot中使用@ControllerAdvice注解詳解的文章就介紹到這了,更多相關SpringBoot的@ControllerAdvice注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot Application事件監(jiān)聽的實現(xiàn)方案
這篇文章主要介紹了SpringBoot Application事件監(jiān)聽的實現(xiàn)方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05Java?IO篇之Reactor?網(wǎng)絡模型的概念
Reactor?模式也叫做反應器設計模式,是一種為處理服務請求并發(fā)提交到一個或者多個服務處理器的事件設計模式,Reactor?模式主要由?Reactor?和處理器?Handler?這兩個核心部分組成,本文給大家介紹Java?IO篇之Reactor?網(wǎng)絡模型的概念,感興趣的朋友一起看看吧2022-01-01java?MongoDB實現(xiàn)列表分頁查詢的示例代碼
本文主要介紹了java?MongoDB實現(xiàn)列表分頁查詢的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07解決IDEA?JDK9沒有module-info.java的問題
這篇文章主要介紹了解決IDEA?JDK9沒有module-info.java的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01