Spring中的@ControllerAdvice注解配合@ExceptionHandler使用实现异常处理
2016-04-05 14:16
851 查看
@ControllerAdvice,是spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强。让我们先看看@ControllerAdvice的实现:
没什么特别之处,该注解使用@Component注解,这样的话当我们使用<context:component-scan>扫描时也能扫描到
即把@ControllerAdvice注解内部使用@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法应用到所有的 @RequestMapping注解的方法。非常简单,不过只有当使用@ExceptionHandler最有用,另外两个用处不大。
接下来看段代码:
如果你的spring-mvc配置文件使用如下方式扫描bean
需要把@ControllerAdvice包含进来,否则不起作用:
该注解非常简单,大多数时候其实只@ExceptionHandler比较有用,其他两个用到的场景非常少,这样可以把异常处理器应用到所有控制器,而不是@Controller注解的单个控制器。
以下为一个实例
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface ControllerAdvice { }
没什么特别之处,该注解使用@Component注解,这样的话当我们使用<context:component-scan>扫描时也能扫描到
/** * 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注解内部使用@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法应用到所有的 @RequestMapping注解的方法。非常简单,不过只有当使用@ExceptionHandler最有用,另外两个用处不大。
接下来看段代码:
@ControllerAdvice public class ControllerAdviceTest { @ModelAttribute public User newUser() { System.out.println("============应用到所有@RequestMapping注解方法,在其执行之前把返回值放入Model"); return new User(); } @InitBinder public void initBinder(WebDataBinder binder) { System.out.println("============应用到所有@RequestMapping注解方法,在其执行之前初始化数据绑定器"); } @ExceptionHandler(UnauthenticatedException.class) @ResponseStatus(HttpStatus.UNAUTHORIZED) public String processUnauthenticatedException(NativeWebRequest request, UnauthenticatedException e) { System.out.println("===========应用到所有@RequestMapping注解的方法,在其抛出UnauthenticatedException异常时执行"); return "viewName"; //返回一个逻辑视图名 } }
如果你的spring-mvc配置文件使用如下方式扫描bean
<context:component-scan base-package="com.sishuok.es" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
需要把@ControllerAdvice包含进来,否则不起作用:
<context:component-scan base-package="com.sishuok.es" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan>
该注解非常简单,大多数时候其实只@ExceptionHandler比较有用,其他两个用到的场景非常少,这样可以把异常处理器应用到所有控制器,而不是@Controller注解的单个控制器。
以下为一个实例
package com.zjr.common.web; //import ... @ControllerAdvice public class WebExceptionHandler { Logger logger = LoggerFactory.getLogger(this.getClass()); @ExceptionHandler({ ServletException.class }) public void http404(HttpServletRequest request, HttpServletResponse response, ServletException e) throws IOException { logger.error("HttpRequest error:{}", request.getRequestURL()); logger.error("HttpRequest error stack :" ,e ); outputMessage(response, ErrorType.HTTP404_ERROR.getErrCode(), ErrorType.HTTP404_ERROR.getErrMsg()); } @ExceptionHandler(BizException.class) public void handleBizException(HttpServletRequest request, HttpServletResponse response, BizException e) throws IOException { logger.error("Biz error occurs during :{}",request.getRequestURI()); logger.error("Biz error stack :",e); outputMessage(response, e.getErrCode(), e.getMessage()); } @ExceptionHandler({ Error.class, Exception.class, Throwable.class }) public void exception(HttpServletRequest request, HttpServletResponse response, Throwable e) throws IOException { logger.error("Unknown error occurs during :{}",request.getRequestURI()); logger.error("Unknown system error stack:", e); outputMessage(response, ErrorType.UNKNOWN_ERROR.getErrCode(),ErrorType.UNKNOWN_ERROR.getErrMsg()); } private void outputMessage(HttpServletResponse response, long errCode, String errMsg) throws IOException { BasicResult result = BasicResult.createFailResult(errCode, errMsg); String json = new JsonMapper().toJson(result); response.setCharacterEncoding("UTF-8"); response.setContentType("text/json"); ServletOutputStream os = response.getOutputStream(); os.write(json.getBytes("utf-8")); } }
相关文章推荐
- java动态代理机制
- Java IDE大比拼
- Struts2 Namespace_命名空间
- CCF 数字排序(java)
- Eclipse使用git最基本流程 - 张万帆
- Synch4j数据同步系统的开源版
- Java并发编程:CountDownLatch、CyclicBarrier和Semaphore
- spring事务管理
- java处理小数点末尾的0
- Maven 下的spring框架(4定时器quartz)
- java反射之获取方法,成员变量,构造方法
- java处理Excel相关笔记
- maven springmvc hibernate shiro 集成(转)
- The method of type must override a superclass method 解决方式
- spring事务管理
- java bigdecimal
- JavaWeb
- Struts2总是跳转到INPUT
- java计算文件32位md5值
- Java多线程-新特性-有返回值的线程