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)文章希望大家以后多多支持腳本之家!
- Spring注解@RestControllerAdvice原理解析
- springboot @Controller和@RestController的區(qū)別及應(yīng)用詳解
- SpringBoot http請(qǐng)求注解@RestController原理解析
- SpringBoot的@RestControllerAdvice作用詳解
- Spring中@RestControllerAdvice注解的使用詳解
- Spring中的@RestController注解詳細(xì)解析
- Spring @RestController注解組合實(shí)現(xiàn)方法解析
- springboot中@RestController注解實(shí)現(xiàn)
- Spring中@RestController注解的使用實(shí)現(xiàn)
相關(guān)文章
Spring?Boot實(shí)現(xiàn)MyBatis動(dòng)態(tài)創(chuàng)建表的操作語句
這篇文章主要介紹了Spring?Boot實(shí)現(xiàn)MyBatis動(dòng)態(tài)創(chuàng)建表,MyBatis提供了動(dòng)態(tài)SQL,我們可以通過動(dòng)態(tài)SQL,傳入表名等信息然組裝成建表和操作語句,本文通過案例講解展示我們的設(shè)計(jì)思路,需要的朋友可以參考下2024-01-01
SpringMVC 參數(shù)綁定之視圖傳參到控制器的實(shí)現(xiàn)代碼
這篇文章主要介紹了SpringMVC 參數(shù)綁定之視圖傳參到控制器的相關(guān)知識(shí),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
Springboot2.1.6集成activiti7出現(xiàn)登錄驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了Springboot2.1.6集成activiti7出現(xiàn)登錄驗(yàn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
解決JSONObject.toJSONString()輸出null的問題
這篇文章主要介紹了解決JSONObject.toJSONString()輸出null的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

