SpringMVC异常处理注解@ExceptionHandler@ControllerAdvice@ResponseStatus

参考:

http://blog.csdn.net/w372426096/article/details/78429132

http://blog.csdn.net/w372426096/article/details/78429141

@ExceptionHandler:统一处理某一类异常,从而能够减少代码重复率和复杂度

源码如下:

复制代码
1 @Target({ElementType.METHOD})
2 @Retention(RetentionPolicy.RUNTIME)
3 @Documented
4 public @interface ExceptionHandler {
5     Class<? extends Throwable>[] value() default {};
6 }
复制代码

注解作用对象为方法,并且在运行时有效,value()可以指定异常类。由该注解注释的方法可以具有灵活的输入参数(详细参见Spring API):

  • 异常参数:包括一般的异常或特定的异常(即自定义异常),如果注解没有指定异常类,会默认进行映射。
  • 请求或响应对象 (Servlet API or Portlet API): 你可以选择不同的类型,如ServletRequest/HttpServletRequest或PortleRequest/ActionRequest/RenderRequest
  • Session对象(Servlet API or Portlet API): HttpSession或PortletSession。
  • WebRequest或NativeWebRequest 
  • Locale
  • InputStream/Reader 
  • OutputStream/Writer 
  • Model

方法返回值可以为:

  • ModelAndView对象
  • Model对象
  • Map对象
  • View对象
  • String对象
  • 还有@ResponseBody、HttpEntity<?>或ResponseEntity<?>,以及void

@ControllerAdvice

源码如下:

复制代码
 1 @Target({ElementType.TYPE})
 2 @Retention(RetentionPolicy.RUNTIME)
 3 @Documented
 4 @Component
 5 public @interface ControllerAdvice {
 6     @AliasFor("basePackages")
 7     String[] value() default {};
 8 
 9     @AliasFor("value")
10     String[] basePackages() default {};
11 
12     Class<?>[] basePackageClasses() default {};
13 
14     Class<?>[] assignableTypes() default {};
15 
16     Class<? extends Annotation>[] annotations() default {};
17 }
复制代码

该注解作用对象为TYPE,包括类、接口和枚举等,在运行时有效,并且可以通过Spring扫描为bean组件。其可以包含由@ExceptionHandler、@InitBinder 和@ModelAttribute标注的方法,可以处理多个Controller类,这样所有控制器的异常可以在一个地方进行处理

@ResponseStatus:可以将某种异常映射为HTTP状态码

 

 


无论是普通的WEB项目,还是用SpringMVC实现的restful服务,都曾经历过下面两个问题:

这里写图片描述

 

  1. @PostMapping(path = "/selectByAcctcode")
  2. public MerAccountQueryResponse selectByAcctcode(@Valid @RequestBody MerAccountQueryRequest request,BindingResult result) {
  3.  
  4. log.info(Constants.REQUEST_MSG, JSON.toJSONString(request));
  5.  
  6. MerAccountQueryResponse response = new MerAccountQueryResponse();
  7.  
  8. try {
  9. Pageable pageable = new PageRequest(request.getPageNum(), request.getPageSize());
  10. response = merAccountService.selectByAcctcode(request, pageable);
  11. // 返回成功报文
  12. MessageUtil.createCommMsg(response);
  13. } catch (BizException e) {
  14. log.error(Constants.BUSINESS_ERROR, e);
  15. // 组织错误报文
  16. MessageUtil.errRetrunInAction(response, e);
  17. } catch (Exception ex) {
  18. log.error(Constants.EXCEPTION_ERROR, ex);
  19. // 组织错误报文
  20. MessageUtil.createErrorMsg(response,ex);
  21. }
  22. log.info(Constants.REPONSE_MSG, JSON.toJSONString(response));
  23. return response;
  24. }

 

当你有100个接口的时候,就得重复100次,如果你觉得代码量也就那么点,copy就copy吧,反正我是代码的搬运工,只是你有曾想过我可以抽取出来吗?

 

这里写图片描述

我们在写Controller的时候,如果没有出现过异常固然没问题,但一旦出现异常了,如果你处理了,那就需要你手动指定跳转到事先定义好的界面,如果你没处理,那将得到是一个非常丑陋的界面,如下:

这里写图片描述

如何避免这种问题呢???

这里写图片描述

这里写图片描述

 

  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4.  
  5. @ExceptionHandler({ ArithmeticException.class })
  6. public String handleArithmeticException(Exception e) {
  7. e.printStackTrace();
  8. return "error";
  9. }
  10.  
  11. @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
  12. @ResponseBody
  13. public String testExceptionHandle(@PathVariable(value = "id") Integer id) {
  14. System.out.println(10 / id);
  15. return id.toString();
  16. }
  17. }


当访问exception/e/0的时候,会抛出ArithmeticException异常,@ExceptionHandler就会处理并响应error.jsp

这里写图片描述

 

  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4.  
  5. @ExceptionHandler({ ArithmeticException.class })
  6. @ResponseBody
  7. public String handleArithmeticException(Exception e) {
  8. e.printStackTrace();
  9. JSONObject jo = new JSONObject();
  10. jo.put("resCode","999999");
  11. jo.put("resMsg","系统异常");
  12. return jo.toString();
  13. }
  14.  
  15. @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
  16. @ResponseBody
  17. public String testExceptionHandle(@PathVariable(value = "id") Integer id) {
  18. System.out.println(10 / id);
  19. return id.toString();
  20. }
  21. }

 

当然实际项目中,并不会像我这里写的这么简陋,我这里只是抛砖引玉,给你一个思路。

这里写图片描述

在实际项目中,可能碰到这种情况,我们提供的服务,调用方并不需要json报文中的消息,调用方只关注响应码,比如200,代表调用正常;404,代表请求资源不存在;502,代表系统异常。。。等等。我们又该如何去做?

这里写图片描述

  1. package com.somnus.exception;
  2.  
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.web.bind.annotation.ResponseStatus;
  5.  
  6. @ResponseStatus(value=HttpStatus.BAD_GATEWAY)
  7. public class HttpStatusException extends RuntimeException {
  8.  
  9. private static final long serialVersionUID = 1L;
  10.  
  11. public HttpStatusException() {
  12. super();
  13. }
  14.  
  15. public HttpStatusException(String message, Throwable cause) {
  16. super(message, cause);
  17. }
  18.  
  19. public HttpStatusException(String message) {
  20. super(message);
  21. }
  22.  
  23. public HttpStatusException(Throwable cause) {
  24. super(cause);
  25. }
  26.  
  27. }
  1. @Controller
  2. @RequestMapping(value = "status")
  3. public class ResponseStatusController {
  4.  
  5. @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
  6. @ResponseBody
  7. public String status(@PathVariable(value = "id") Integer id){
  8. if(id % 2 != 0){
  9. throw new HttpStatusException();
  10. }
  11. return id.toString();
  12. }
  13. }

 

 

效果如下:

这里写图片描述

另外这里不得不提一点需要注意的,不要轻易把@ResponseStatus修饰目标方法,因为无论它执行方法过程中有没有异常产生,用户都会得到异常的界面,而目标方法正常执行。

  1. package com.somnus.controller;
  2.  
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import org.springframework.web.bind.annotation.ResponseStatus;
  10.  
  11. import com.somnus.exception.HttpStatusException;
  12.  
  13. @Controller
  14. @RequestMapping(value = "status")
  15. public class ResponseStatusController {
  16.  
  17. /**
  18. * ResponseStatus修饰目标方法,无论它执行方法过程中有没有异常产生,用户都会得到异常的界面。而目标方法正常执行
  19. * @param id
  20. * @return
  21. */
  22. @RequestMapping(value = "e2/{id}", method = { RequestMethod.GET })
  23. @ResponseStatus(value=HttpStatus.BAD_GATEWAY)
  24. @ResponseBody
  25. public String status2(@PathVariable(value = "id") Integer id){
  26. System.out.println(10 / id);
  27. return id.toString();
  28. }
  29.  
  30. }

 

 

可以看到哪怕是响应2了,但是响应码其实还是502

这里写图片描述


这里写图片描述

如果我们要给jdk自带的异常提供一个响应码呢,我们又不可能去改源码,这时候@ResponseStatus就得配和@ControllerAdvice一起使用了,如下:

 

  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4.  
  5. @ExceptionHandler({ NullPointerException.class })
  6. @ResponseStatus(value=HttpStatus.NOT_FOUND)
  7. public void handleNullPointerException(Exception e) {
  8. e.printStackTrace();
  9. }
  10.  
  11. @RequestMapping(value = "e3/{id}", method = { RequestMethod.GET })
  12. @ResponseBody
  13. public String testExceptionHandle3(@PathVariable(value = "id") Integer id) {
  14. List<String> list = 4 % id == 0 ? null : Arrays.asList(new String[]{"a","b","c","d"});
  15. return list.get(id);
  16. }
  17. }

 

当我们抛出NullPointerException异常的时候会发生什么呢

这里写图片描述

这里写图片描述

    • 当一个Controller中有多个@ExceptionHandler注解出现时,那么异常被哪个方法捕捉呢?这就存在一个优先级的问题,@ExceptionHandler的优先级是:在异常的体系结构中,哪个异常与目标方法抛出的异常血缘关系越紧密,就会被哪个捕捉到

    • @ExceptionHandler这个只会是在当前的Controller里面起作用,如果想在所有的Controller里面统一处理异常的话,可以用@ControllerAdvice来创建一个专门处理的类,我们在下一篇做介绍。

 

@ControllerAdvice,是Spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强。让我们先看看@ControllerAdvice的实现:

  1. package org.springframework.web.bind.annotation;
  2.  
  3. @Target(ElementType.TYPE)
  4. @Retention(RetentionPolicy.RUNTIME)
  5. @Documented
  6. @Component
  7. public @interface ControllerAdvice {
  8.  
  9. @AliasFor("basePackages")
  10. String[] value() default {};
  11.  
  12. @AliasFor("value")
  13. String[] basePackages() default {};
  14.  
  15. Class<?>[] basePackageClasses() default {};
  16.  
  17. Class<?>[] assignableTypes() default {};
  18.  
  19. Class<? extends Annotation>[] annotations() default {};

 

 

没什么特别之处,该注解使用@Component注解,这样的话当我们使用<context:component-scan>扫描时也能扫描到。

再一起看看官方提供的comment。

 

 

大致意思是:

  • @ControllerAdvice是一个@Component,用于定义@ExceptionHandler@InitBinder@ModelAttribute方法,适用于所有使用@RequestMapping方法。

  • Spring4之前,@ControllerAdvice在同一调度的Servlet中协助所有控制器。Spring4已经改变:@ControllerAdvice支持配置控制器的子集,而默认的行为仍然可以利用。

  • 在Spring4中, @ControllerAdvice通过annotations()basePackageClasses()basePackages()方法定制用于选择控制器子集。

不过据经验之谈,只有配合@ExceptionHandler最有用,其它两个不常用。

这里写图片描述

在SpringMVC重要注解(一)@ExceptionHandler@ResponseStatus我们提到,如果单使用@ExceptionHandler,只能在当前Controller中处理异常。但当配合@ControllerAdvice一起使用的时候,就可以摆脱那个限制了。

 

  1. package com.somnus.advice;
  2.  
  3. import org.springframework.web.bind.annotation.ControllerAdvice;
  4. import org.springframework.web.bind.annotation.ExceptionHandler;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6.  
  7. @ControllerAdvice
  8. public class ExceptionAdvice {
  9.  
  10. @ExceptionHandler({ ArrayIndexOutOfBoundsException.class })
  11. @ResponseBody
  12. public String handleArrayIndexOutOfBoundsException(Exception e) {
  13. e.printStackTrace();
  14. return "testArrayIndexOutOfBoundsException";
  15. }
  16.  
  17. }
  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4.  
  5. @RequestMapping(value = "e2/{id}", method = { RequestMethod.GET })
  6. @ResponseBody
  7. public String testExceptionHandle2(@PathVariable(value = "id") Integer id) {
  8. List<String> list = Arrays.asList(new String[]{"a","b","c","d"});
  9. return list.get(id-1);
  10. }
  11.  
  12. }



 

当我们访问http://localhost:8080/SpringMVC/exception/e2/5的时候会抛出ArrayIndexOutOfBoundsException异常,这时候定义在@ControllerAdvice中的@ExceptionHandler就开始发挥作用了。

如果我们想定义一个处理全局的异常

  1. package com.somnus.advice;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4.  
  5. import org.springframework.core.annotation.AnnotationUtils;
  6. import org.springframework.web.bind.annotation.ControllerAdvice;
  7. import org.springframework.web.bind.annotation.ExceptionHandler;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import org.springframework.web.bind.annotation.ResponseStatus;
  10.  
  11. @ControllerAdvice
  12. public class ExceptionAdvice {
  13.  
  14. @ExceptionHandler({ Exception.class })
  15. @ResponseBody
  16. public String handException(HttpServletRequest request ,Exception e) throws Exception {
  17. e.printStackTrace();
  18.  
  19. return e.getMessage();
  20. }
  21.  
  22. }

 

乍一眼看上去毫无问题,但这里有一个纰漏,由于Exception是异常的父类,如果你的项目中出现过在自定义异常中使用@ResponseStatus的情况,你的初衷是碰到那个自定义异常响应对应的状态码,而这个控制器增强处理类,会首先进入,并直接返回,不会再有@ResponseStatus的事情了,这里为了解决这种纰漏,我提供了一种解决方式。

 

  1. package com.somnus.advice;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4.  
  5. import org.springframework.core.annotation.AnnotationUtils;
  6. import org.springframework.web.bind.annotation.ControllerAdvice;
  7. import org.springframework.web.bind.annotation.ExceptionHandler;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import org.springframework.web.bind.annotation.ResponseStatus;
  10.  
  11. @ControllerAdvice
  12. public class ExceptionAdvice {
  13.  
  14.  
  15. @ExceptionHandler({ Exception.class })
  16. @ResponseBody
  17. public String handException(HttpServletRequest request ,Exception e) throws Exception {
  18. e.printStackTrace();
  19. //If the exception is annotated with @ResponseStatus rethrow it and let
  20. // the framework handle it - like the OrderNotFoundException example
  21. // at the start of this post.
  22. // AnnotationUtils is a Spring Framework utility class.
  23. if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null){
  24. throw e;
  25. }
  26. // Otherwise setup and send the user to a default error-view.
  27. /*ModelAndView mav = new ModelAndView();
  28. mav.addObject("exception", e);
  29. mav.addObject("url", request.getRequestURL());
  30. mav.setViewName(DEFAULT_ERROR_VIEW);
  31. return mav;*/
  32. return e.getMessage();
  33. }
  34.  
  35. }


如果碰到了某个自定义异常加上了@ResponseStatus,就继续抛出,这样就不会让自定义异常失去加上@ResponseStatus的初衷。

posted on 2018-01-28 18:33  已停更  阅读(5575)  评论(1编辑  收藏  举报