SpringBoot常用注解@RestControllerAdvice詳解
@RestControllerAdvice是什么
@RestControllerAdvice是一個(gè)組合注解,由@ControllerAdvice、@ResponseBody組成,而@ControllerAdvice繼承了@Component,因此@RestControllerAdvice本質(zhì)上是個(gè)Component,用于定義@ExceptionHandler,@InitBinder和@ModelAttribute方法,適用于所有使用@RequestMapping方法。
@RestControllerAdvice的特點(diǎn)
- 通過@ControllerAdvice注解可以將對(duì)于控制器的全局配置放在同一個(gè)位置。
- 注解了@RestControllerAdvice的類的方法可以使用@ExceptionHandler、@InitBinder、@ModelAttribute注解到方法上。
- @RestControllerAdvice注解將作用在所有注解了@RequestMapping的控制器的方法上。
- @ExceptionHandler:用于指定異常處理方法。當(dāng)與@RestControllerAdvice配合使用時(shí),用于全局處理控制器里的異常。
- @InitBinder:用來設(shè)置WebDataBinder,用于自動(dòng)綁定前臺(tái)請(qǐng)求參數(shù)到Model中。
- @ModelAttribute:本來作用是綁定鍵值對(duì)到Model中,當(dāng)與@ControllerAdvice配合使用時(shí),可以讓全局的@RequestMapping都能獲得在此處設(shè)置的鍵值對(duì)
@ControllerAdvice public class GlobalController{ //(1)全局?jǐn)?shù)據(jù)綁定 //應(yīng)用到所有@RequestMapping注解方法 //此處將鍵值對(duì)添加到全局,注解了@RequestMapping的方法都可以獲得此鍵值對(duì) @ModelAttribute public void addUser(Model model) { model.addAttribute("msg", "此處將鍵值對(duì)添加到全局,注解了@RequestMapping的方法都可以獲得此鍵值對(duì)"); } //(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 會(huì)被該 @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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用@RestController處理GET和POST請(qǐng)求的代碼詳解
- SpringBoot中@RestControllerAdvice @ExceptionHandler異常統(tǒng)一處理類失效原因分析
- SpringBoot中@RestControllerAdvice注解的使用
- SpringBoot的@RestControllerAdvice作用詳解
- SpringBoot中@RestControllerAdvice注解實(shí)現(xiàn)全局異常處理類
- SpringBoot中的@RestControllerAdvice注解詳解
- SpringBoot?@RestControllerAdvice注解對(duì)返回值統(tǒng)一封裝的處理方法
- springboot @Controller和@RestController的區(qū)別及應(yīng)用詳解
- SpringBoot http請(qǐng)求注解@RestController原理解析
- springboot中@RestController注解實(shí)現(xiàn)
相關(guān)文章
SpringBoot2.X Devtools熱部署實(shí)現(xiàn)解析
這篇文章主要介紹了SpringBoot2.X Devtools熱部署實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Spring Boot2+JPA之悲觀鎖和樂觀鎖實(shí)戰(zhàn)教程
這篇文章主要介紹了Spring Boot2+JPA之悲觀鎖和樂觀鎖實(shí)戰(zhàn)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Thymeleaf渲染網(wǎng)頁時(shí)中文亂碼的問題及解決
這篇文章主要介紹了Thymeleaf渲染網(wǎng)頁時(shí)中文亂碼的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Springboot自定義mvc組件如何實(shí)現(xiàn)
這篇文章主要介紹了Springboot自定義mvc組件如何實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11SpringBoot環(huán)境屬性占位符解析和類型轉(zhuǎn)換方式
這篇文章主要介紹了SpringBoot環(huán)境屬性占位符解析和類型轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11