十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要介紹“spring boot中@ControllerAdvice的用法”,在日常操作中,相信很多人在spring boot中@ControllerAdvice的用法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”spring boot中@ControllerAdvice的用法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

網(wǎng)站建設哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、成都微信小程序、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了袁州免費建站歡迎大家使用!
@ControllerAdvice 是spring 3.2提供的新注解,他是一個controller增強器,可以對controller中使用到@RequestMapping注解的方法做邏輯處理,用法可分為以下三種:
局異常處理
全局數(shù)據(jù)綁定
全局數(shù)據(jù)預處理
靈活使用這三個功能,可以幫助我們簡化很多工作,需要注意的是,這是 SpringMVC 提供的功能,在 Spring Boot 中可以直接使用,下面分別來看。
使用 @ControllerAdvice 實現(xiàn)全局異常處理,只需要定義類,添加該注解即可定義方式如下:
@ControllerAdvice
public class MyGlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ModelAndView customException(Exception e) {
ModelAndView mv = new ModelAndView();
mv.addObject("message", e.getMessage());
mv.setViewName("myerror");
return mv;
}
}在該類中,可以定義多個方法,不同的方法處理不同的異常,例如專門處理空指針的方法、專門處理數(shù)組越界的方法...,也可以直接向上面代碼一樣,在一個方法中處理所有的異常信息。
@ExceptionHandler 注解用來指明異常的處理類型,即如果這里指定為 NullpointerException,則數(shù)組越界異常就不會進到這個方法中來。
全局數(shù)據(jù)綁定功能可以用來做一些初始化的數(shù)據(jù)操作,我們可以將一些公共的數(shù)據(jù)定義在添加了 @ControllerAdvice 注解的類中,這樣,在每一個 Controller 的接口中,就都能夠訪問到這些數(shù)據(jù)。
使用步驟,首先定義全局數(shù)據(jù),如下:
@ControllerAdvice
public class MyGlobalExceptionHandler {
@ModelAttribute(name = "md")
public Map mydata() {
HashMap 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ù):
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(Model model) {
Map map = model.asMap();
System.out.println(map);
int i = 1 / 0;
return "hello controller advice";
}
} 考慮我有兩個實體類,Book 和 Author,分別定義如下:
public class Book {
private String name;
private Long price;
//getter/setter
}
public class Author {
private String name;
private Integer age;
//getter/setter
}此時,如果我定義一個數(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和相關(guān)的參數(shù),在方法中,給參數(shù)添加一個 b 前綴,即請求參數(shù)要有b前綴.
3.發(fā)送請求
請求發(fā)送時,通過給不同對象的參數(shù)添加不同的前綴,可以實現(xiàn)參數(shù)的區(qū)分.
到此,關(guān)于“spring boot中@ControllerAdvice的用法”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)站標題:springboot中@ControllerAdvice的用法
網(wǎng)址分享:http://m.jiaotiyi.com/article/iihgho.html