javascript
Spring3.2新注解@ControllerAdvice
@ControllerAdvice,是spring3.2提供的新注解,從名字上可以看出大體意思是控制器增強(qiáng)。
注解@ControllerAdvice的代碼:
該注解使用了@Component注解,這樣的話當(dāng)我們使用<context:component-scan>掃描時(shí)也能掃描到。
其javadoc定義:
/** * Indicates the annotated class assists a "Controller". * * <p>Serves as a specialization of {@link Component @Component}, allowing for * implementation classes to be autodetected through classpath scanning. * * <p>It is typically used to define {@link ExceptionHandler @ExceptionHandler}, * {@link InitBinder @InitBinder}, and {@link ModelAttribute @ModelAttribute} * methods that apply to all {@link RequestMapping @RequestMapping} methods. * * @author Rossen Stoyanchev * @since 3.2 */即把@ControllerAdvice注解內(nèi)部使用@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法應(yīng)用到所有的 @RequestMapping注解的方法。非常簡(jiǎn)單,不過只有當(dāng)使用@ExceptionHandler最有用,另外兩個(gè)用處不大。
如下面代碼中的processUnauthenticatedException方法就會(huì)應(yīng)用到有@RequestMapping注解的方法。
代碼:
@ControllerAdvice public class ControllerAdviceTest { @ModelAttribute public User newUser() { System.out.println("============應(yīng)用到所有@RequestMapping注解方法,在其執(zhí)行之前把返回值放入Model"); return new User(); } @InitBinder public void initBinder(WebDataBinder binder) { System.out.println("============應(yīng)用到所有@RequestMapping注解方法,在其執(zhí)行之前初始化數(shù)據(jù)綁定器"); } @ExceptionHandler(UnauthenticatedException.class) @ResponseStatus(HttpStatus.UNAUTHORIZED) public String processUnauthenticatedException(NativeWebRequest request, UnauthenticatedException e) { System.out.println("===========應(yīng)用到所有@RequestMapping注解的方法,在其拋出UnauthenticatedException異常時(shí)執(zhí)行"); return "viewName"; //返回一個(gè)邏輯視圖名 } }如果你的spring-mvc配置文件使用如下方式掃描bean
Java代碼 收藏代碼
需要把@ControllerAdvice包含進(jìn)來,否則不起作用:
Java代碼 收藏代碼
1、@ModelAttribute注解的方法作用跟springMVC中的作用是一樣的,只不過此處是對(duì)所有的@RequestMapping注解的方法都起作用。當(dāng)需要設(shè)置全局?jǐn)?shù)據(jù)時(shí)比較有用。
2、@InitBinder注解的方法作用同1類似。當(dāng)需要全局注冊(cè)時(shí)比較有用。
3、@ExceptionHandler,異常處理器,此注解的作用是當(dāng)出現(xiàn)其定義的異常時(shí)進(jìn)行處理的方法,其可以使用springmvc提供的數(shù)據(jù)綁定,比如注入HttpServletRequest等,還可以接受一個(gè)當(dāng)前拋出的Throwable對(duì)象。
該注解非常簡(jiǎn)單,大多數(shù)時(shí)候其實(shí)只@ExceptionHandler比較有用,其他兩個(gè)用到的場(chǎng)景非常少,這樣可以把異常處理器應(yīng)用到所有控制器,而不是@Controller注解的單個(gè)控制器。
來源:http://jinnianshilongnian.iteye.com/blog/1866350
我在實(shí)際項(xiàng)目中遇到的:
@ControllerAdvice //增強(qiáng)注解 @ResponseBody public class ExceptionAdvice {/** * 400 - Bad Request */ @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(HttpMessageNotReadableException.class) public BaseResult handleHttpMessageNotReadableException(HttpMessageNotReadableException e) { // logger.error("參數(shù)解析失敗", e); return new BaseResult().failure("could_not_read_json"); } /** * 405 - Method Not Allowed */ @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public BaseResult handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { // logger.error("不支持當(dāng)前請(qǐng)求方法", e); return new BaseResult().failure("request_method_not_supported"); } /** * 415 - Unsupported Media Type */ @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE) @ExceptionHandler(HttpMediaTypeNotSupportedException.class) public BaseResult handleHttpMediaTypeNotSupportedException(Exception e) { // logger.error("不支持當(dāng)前媒體類型", e); return new BaseResult().failure("content_type_not_supported"); } /** * 500 - Internal Server Error */ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(Exception.class) public BaseResult handleException(Exception e) { // logger.error("服務(wù)運(yùn)行異常", e); return new BaseResult().failure(e.getLocalizedMessage()); } }上面使用了@ControllerAdvice注解的類里的方法,會(huì)在控制器代碼里出現(xiàn)相應(yīng)錯(cuò)誤時(shí)調(diào)用,然后返回一個(gè)結(jié)果到前端。
總結(jié)
以上是生活随笔為你收集整理的Spring3.2新注解@ControllerAdvice的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java获得泛型类中T的实例
- 下一篇: gradle idea java ssm