spring boot全局异常处理
1、通过intellij idea创建spring boot项目,如下图所示

2、添加maven依赖,如下图所示
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

3、创建一个测试controller方法,然后返回异常,代码如下
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping("")
public Map test(@RequestBody Map map) throws Exception {
if (1==1){
throw new Exception("1111111111");
}
return map;
}
}

4、创建全局异常处理器,使用@ControllerAdvice,
@ExceptionHandler等注解

5、启动程序,访问rest接口

6、全局异常处理器被执行

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:102
阅读量:103
阅读量:56
阅读量:159
阅读量:188