赵计刚
每天进步一点点
随笔 - 234,  文章 - 0,  评论 - 243,  阅读 - 166万

一、单个controller范围的异常处理

复制代码
 1 package com.xxx.secondboot.web;
 2 
 3 import org.springframework.web.bind.annotation.ExceptionHandler;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 import com.xxx.secondboot.exception.MyExceptionResponse;
 9 
10 import io.swagger.annotations.Api;
11 
12 @Api("测试controllerAdvice和全局异常处理")
13 @RestController
14 @RequestMapping("/advice1")
15 public class AdviceController {
16 
17     @RequestMapping(value = "/test1", method = RequestMethod.GET)
18     public String test1() {
19         throw new RuntimeException("advice1 - exception1");
20     }
21 
22     @RequestMapping(value = "/test2", method = RequestMethod.GET)
23     public String test2() {
24         throw new RuntimeException("advice1 - exception2");
25     }
26 
27     @ExceptionHandler(RuntimeException.class)
28     public MyExceptionResponse exceptionHandler() {
29         MyExceptionResponse resp = new MyExceptionResponse();
30         resp.setCode(300);
31         resp.setMsg("exception-Handler");
32         return resp;
33     }
34 
35 }
复制代码

说明:

  • 在controller中加入被@ExceptionHandler修饰的类即可(在该注解中指定该方法需要处理的那些异常类)
  • 该异常处理方法只在当前的controller中起作用

二、全部controller范围内起作用的异常处理(全局异常处理)

1、全局异常处理类

复制代码
 1 package com.xxx.secondboot.web;
 2 
 3 import javax.servlet.http.HttpServletResponse;
 4 
 5 import org.springframework.web.bind.annotation.ControllerAdvice;
 6 import org.springframework.web.bind.annotation.ExceptionHandler;
 7 import org.springframework.web.bind.annotation.ResponseBody;
 8 import org.springframework.web.bind.annotation.RestController;
 9 
10 import com.xxx.secondboot.exception.MyExceptionResponse;
11 import com.xxx.secondboot.exception.MyRuntimeException;
12 
13 //@ControllerAdvice(annotations=RestController.class)
14 //@ControllerAdvice(basePackages={"com.xxx","com.ooo"})
15 @ControllerAdvice
16 public class GlobalExceptionHandler {
17     @ExceptionHandler(RuntimeException.class)
18     //    @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class})
19     //    @ExceptionHandler//处理所有异常
20     @ResponseBody //在返回自定义相应类的情况下必须有,这是@ControllerAdvice注解的规定
21     public MyExceptionResponse exceptionHandler(RuntimeException e, HttpServletResponse response) {
22         MyExceptionResponse resp = new MyExceptionResponse();
23         resp.setCode(300);
24         resp.setMsg("exception-Handler");
25         //        response.setStatus(600);
26         return resp;
27     }
28 }
复制代码

说明:

  • @ControllerAdvice是controller的一个辅助类,最常用的就是作为全局异常处理的切面类
  • @ControllerAdvice可以指定扫描范围
  • @ControllerAdvice约定了几种可行的返回值,如果是直接返回model类的话,需要使用@ResponseBody进行json转换
    • 返回String,表示跳到某个view
    • 返回modelAndView
    • 返回model + @ResponseBody

2、controller

复制代码
 1 package com.xxx.secondboot.web;
 2 
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 import org.springframework.web.bind.annotation.RequestMethod;
 5 import org.springframework.web.bind.annotation.RestController;
 6 
 7 import io.swagger.annotations.Api;
 8 
 9 @Api("测试controllerAdvice和全局异常处理")
10 @RestController
11 @RequestMapping("/advice1")
12 public class AdviceController {
13 
14     @RequestMapping(value = "/test1", method = RequestMethod.GET)
15     public String test1() {
16         throw new RuntimeException("advice1 - exception1");
17     }
18 
19     @RequestMapping(value = "/test2", method = RequestMethod.GET)
20     public String test2() {
21         throw new RuntimeException("advice1 - exception2");
22     }
23 
24     //    @ExceptionHandler(RuntimeException.class)
25     //    public MyExceptionResponse exceptionHandler() {
26     //        MyExceptionResponse resp = new MyExceptionResponse();
27     //        resp.setCode(300);
28     //        resp.setMsg("exception-Handler");
29     //        return resp;
30     //    }
31 
32 }
复制代码

注意:

  • 同一个异常被局部范围异常处理器和全局范围异常处理器同时覆盖,会选择小范围的局部范围处理器
  • 同一个异常被小范围的异常类和大范围的异常处理器同时覆盖,会选择小范围的异常处理器

参考自:

  • http://jinnianshilongnian.iteye.com/blog/1866350 开涛的@ControllerAdvice(三个作用)
  • http://www.tuicool.com/articles/fA7nuii                springboot约定的异常处理体系
  • https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc springMVC异常处理体系
  • http://www.baeldung.com/2013/01/31/exception-handling-for-rest-with-spring-3-2/ springMVC异常处理体系
posted on   赵计刚  阅读(25846)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
阅读排行:
· 几个技巧,教你去除文章的 AI 味!
· 系统高可用的 10 条军规
· 对象命名为何需要避免'-er'和'-or'后缀
· 关于普通程序员该如何参与AI学习的三个建议以及自己的实践
· AI与.NET技术实操系列(八):使用Catalyst进行自然语言处理

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示