SpringBoot常用注解@RestControllerAdvice詳解
@RestControllerAdvice是什么
@RestControllerAdvice是一個(gè)組合注解,由@ControllerAdvice、@ResponseBody組成,而@ControllerAdvice繼承了@Component,因此@RestControllerAdvice本質(zhì)上是個(gè)Component,用于定義@ExceptionHandler,@InitBinder和@ModelAttribute方法,適用于所有使用@RequestMapping方法。
@RestControllerAdvice的特點(diǎn)
- 通過@ControllerAdvice注解可以將對于控制器的全局配置放在同一個(gè)位置。
- 注解了@RestControllerAdvice的類的方法可以使用@ExceptionHandler、@InitBinder、@ModelAttribute注解到方法上。
- @RestControllerAdvice注解將作用在所有注解了@RequestMapping的控制器的方法上。
- @ExceptionHandler:用于指定異常處理方法。當(dāng)與@RestControllerAdvice配合使用時(shí),用于全局處理控制器里的異常。
- @InitBinder:用來設(shè)置WebDataBinder,用于自動(dòng)綁定前臺請求參數(shù)到Model中。
- @ModelAttribute:本來作用是綁定鍵值對到Model中,當(dāng)與@ControllerAdvice配合使用時(shí),可以讓全局的@RequestMapping都能獲得在此處設(shè)置的鍵值對
@ControllerAdvice public class GlobalController{ //(1)全局?jǐn)?shù)據(jù)綁定 //應(yīng)用到所有@RequestMapping注解方法 //此處將鍵值對添加到全局,注解了@RequestMapping的方法都可以獲得此鍵值對 @ModelAttribute public void addUser(Model model) { model.addAttribute("msg", "此處將鍵值對添加到全局,注解了@RequestMapping的方法都可以獲得此鍵值對"); } //(2)全局?jǐn)?shù)據(jù)預(yù)處理 //應(yīng)用到所有@RequestMapping注解方法,在其執(zhí)行之前初始化數(shù)據(jù)綁定器 //用來設(shè)置WebDataBinder @InitBinder("user") public void initBinder(WebDataBinder binder) { } // (3)全局異常處理 //應(yīng)用到所有@RequestMapping注解的方法,在其拋出Exception異常時(shí)執(zhí)行 //定義全局異常處理,value屬性可以過濾攔截指定異常,此處攔截所有的Exception @ExceptionHandler(Exception.class) public String handleException(Exception e) { return "error"; } }
@ControllerAdvice可以指定 Controller 范圍
- basePackages: 指定一個(gè)或多個(gè)包,這些包及其子包下的所有 Controller 都被該 @ControllerAdvice 管理
@RestControllerAdvice(basePackages={"top.onething"}) @Slf4j public class ExceptionHandlerAdvice { @ExceptionHandler(Exception.class) public String handleException(Exception e) { return "error"; } }
- basePackageClasses: 是 basePackages 的一種變形,指定一個(gè)或多個(gè) Controller 類,這些類所屬的包及其子包下的所有 Controller 都被該 @ControllerAdvice 管理
@RestControllerAdvice(basePackageClasses={TestController.class}) @Slf4j public class ExceptionHandlerAdvice { @ExceptionHandler(Exception.class) public String handleException(Exception e) { return "error"; } }
- assignableTypes: 指定一個(gè)或多個(gè) Controller 類,這些類被該 @ControllerAdvice 管理
@RestControllerAdvice(assignableTypes={TestController.class}) @Slf4j public class ExceptionHandlerAdvice { @ExceptionHandler(Exception.class) public String handleException(Exception e) { return "error"; } }
- annotations: 指定一個(gè)或多個(gè)注解,被這些注解所標(biāo)記的 Controller 會被該 @ControllerAdvice 管理
@ControllerAdvice(annotations = {TestAnnotation.class}) @Slf4j public class ExceptionHandlerAdvice { @ExceptionHandler(Exception.class) public String handleException(Exception e) { return "error"; } }
到此這篇關(guān)于SpringBoot常用注解@RestControllerAdvice詳解的文章就介紹到這了,更多相關(guān)@RestControllerAdvice注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot中@RestControllerAdvice注解實(shí)現(xiàn)全局異常處理類
- springboot的統(tǒng)一異常處理,使用@RestControllerAdvice詳解
- SpringBoot項(xiàng)目中@RestControllerAdvice全局異常失效問題的解決
- SpringBoot中@RestControllerAdvice @ExceptionHandler異常統(tǒng)一處理類失效原因分析
- SpringBoot中@RestControllerAdvice注解的使用
- SpringBoot的@RestControllerAdvice作用詳解
- SpringBoot中的@RestControllerAdvice注解詳解
- SpringBoot?@RestControllerAdvice注解對返回值統(tǒng)一封裝的處理方法
- SpringBoot中@RestControllerAdvice 全局異常處理的實(shí)現(xiàn)
相關(guān)文章
Spring Boot插件spring tool suite安裝及使用詳解
這篇文章主要介紹了Spring Boot插件spring tool suite安裝及使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08基于Struts2實(shí)現(xiàn)防止表單重復(fù)提交
這篇文章主要介紹了基于Struts2實(shí)現(xiàn)防止表單重復(fù)提交,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10MyBatis深入解讀動(dòng)態(tài)SQL的實(shí)現(xiàn)
動(dòng)態(tài) SQL 是 MyBatis 的強(qiáng)大特性之一。如果你使用過 JDBC 或其它類似的框架,你應(yīng)該能理解根據(jù)不同條件拼接 SQL 語句有多痛苦,例如拼接時(shí)要確保不能忘記添加必要的空格,還要注意去掉列表最后一個(gè)列名的逗號。利用動(dòng)態(tài) SQL,可以徹底擺脫這種痛苦2022-04-04詳解JavaFX桌面應(yīng)用開發(fā)-Group(容器組)
這篇文章主要介紹了JavaFX桌面應(yīng)用開發(fā)-Group(容器組),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04JAVA中常用的設(shè)計(jì)模式:單例模式,工廠模式,觀察者模式
設(shè)計(jì)模式(Design pattern)代表了最佳的實(shí)踐,通常被有經(jīng)驗(yàn)的面向?qū)ο蟮能浖_發(fā)人員所采用。設(shè)計(jì)模式是軟件開發(fā)人員在軟件開發(fā)過程中面臨的一般問題的解決方案。這些解決方案是眾多軟件開發(fā)人員經(jīng)過相當(dāng)長的一段時(shí)間的試驗(yàn)和錯(cuò)誤總結(jié)出來的。2020-04-04