统一异常处理@ExceptionHandler

转自:

https://blog.csdn.net/liujia120103/article/details/75126124/

一、如何设置全局的异常处理
用@RequestBody,@ResponseBody,不费吹灰之力就解决了JSon自动绑定。
接着就发现,如果遇到RuntimeException,需要给出一个默认返回JSON,有以下三种方式:

1.当这个Controller中任何一个方法发生异常,一定会被这个方法拦截到。然后,输出日志。封装Map并返回,页面上得到status为false。

代码如下:

复制代码
 1 @Controller  
 2 public class AccessController {  
 3   
 4     /** 
 5      * 异常页面控制 
 6      *  
 7      * @param runtimeException 
 8      * @return 
 9      */  
10     @ExceptionHandler(RuntimeException.class)  
11     public @ResponseBody  
12     Map<String,Object> runtimeExceptionHandler(RuntimeException runtimeException) {  
13         logger.error(runtimeException.getLocalizedMessage());  
14   
15         Map model = new TreeMap();  
16         model.put("status", false);  
17         return model;  
18     }  
19   
20 } 
复制代码

2.返回到错误界面

代码如下:

复制代码
 1     @Controller  
 2     public class AccessController {  
 3         /** 
 4          * 异常页面控制 
 5          *  
 6          * @param runtimeException 
 7          * @return 
 8          */  
 9         @ExceptionHandler(RuntimeException.class)  
10         public String runtimeExceptionHandler(RuntimeException runtimeException,  
11                 ModelMap modelMap) {  
12             logger.error(runtimeException.getLocalizedMessage());  
13       
14             modelMap.put("status", IntegralConstant.FAIL_STATUS);  
15             return "exception";  
16         }  
17     }  
复制代码

3.使用 @ControllerAdvice,不用任何的配置,只要把这个类放在项目中,Spring能扫描到的地方。就可以实现全局异常的回调。

代码如下:

复制代码
    @ControllerAdvice  
    public class SpringExceptionHandler{  
      /** 
         * 全局处理Exception 
         * 错误的情况下返回500 
         * @param ex 
         * @param req 
         * @return 
         */  
        @ExceptionHandler(value = {Exception.class})  
        public ResponseEntity<Object> handleOtherExceptions(final Exception ex, final WebRequest req) {  
            TResult tResult = new TResult();  
            tResult.setStatus(CodeType.V_500);  
            tResult.setErrorMessage(ex.getMessage());  
            return new ResponseEntity<Object>(tResult,HttpStatus.OK);  
        }  
      
    }  

二、@ExceptionHandler注解

直接在Controller里面加上用@ExceptionHandler标注一个处理异常的方法像下面这样子

  1.  
    @ExceptionHandler(MissingServletRequestParameterException.class)
  2.  
    @ResponseStatus(HttpStatus.BAD_REQUEST)
  3.  
    public void processMethod(MissingServletRequestParameterException ex,HttpServletRequest request ,HttpServletResponse response) throws IOException {
  4.  
    System.out.println("抛异常了!"+ex.getLocalizedMessage());
  5.  
    logger.error("抛异常了!"+ex.getLocalizedMessage());
  6.  
    response.getWriter().printf(ex.getMessage());
  7.  
    response.flushBuffer();
  8.  
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

这样,Controller里面的方法抛出了MissingServletRequestParameterException异常就会执行上面的这个方法来进行异常处理。 
如下面的代码:

  1.  
    @RequestMapping("/index")
  2.  
    public String index(@MyUser User user,@RequestParam String id,ModelMap modelMap){
  3.  
    return "login";
  4.  
    }
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

如果我没有传入id值,那么就会抛出MissingServletRequestParameterException的异常,就会被上面的异常处理方法处理。

上面的@ExceptionHandler(MissingServletRequestParameterException.class)这个注解的value的值是一个Class[]类型的,这里的ExceptionClass是你自己指定的,你也可以指定多个需要处理的异常类型,比如这样@ExceptionHandler(value = {MissingServletRequestParameterException.class,BindException.class}),这样就会处理多个异常了。

但这个只会是在当前的Controller里面起作用,如果想在所有的Controller里面统一处理异常的话,可以用@ControllerAdvice来创建一个专门处理的类。如一中的3所述。

三、@ControllerAdvice注解

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

这里写图片描述

没什么特别之处,该注解使用@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.  
    @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.  
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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

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

这里写图片描述

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

这里写图片描述

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

原文地址:https://www.cnblogs.com/heyanan/p/13389425.html