springboot全局异常处理代码实例

  

这篇文章主要介绍了springboot全局异常处理代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

  

前言:
  

  

开发中异常的处理必不可少,常用的就是扔和尝试,这样一个项目到最后会出现很多冗余的代码,使用全局异常处理避免过多冗余代码。

  

全局异常处理:
  

  

1, pom依赖(延续上一章代码):

        & lt; dependencies>   & lt; !——春天引导Web依赖——比;   & lt; !——Web依赖,包含了spring-boot-starter-validation依赖——比;   & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-web   & lt;/dependency>   & lt; !——春天引导测试依赖——比;   & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-test   & lt; scope> test   & lt;/dependency>   & lt; !——spring-boot整合mybatis——比;   & lt; dependency>   & lt; groupId> org.mybatis.spring.boot   & lt; artifactId> mybatis-spring-boot-starter   & lt; version> 1.3.2   & lt;/dependency>   & lt; !——mysql驱动——比;   & lt; dependency>   & lt; groupId> mysql   & lt; artifactId> mysql-connector-java   & lt; scope> runtime   & lt;/dependency>   & lt; !——阿里巴巴的德鲁伊教团员数据库连接池——比;   & lt; dependency>   & lt; groupId> com.alibaba   & lt; artifactId> druid-spring-boot-starter   & lt; version> 1.1.0   & lt;/dependency>   & lt; !——lombok依赖——比;   & lt; dependency>   & lt; groupId> org.projectlombok   & lt; artifactId> lombok   & lt; version> 1.16.18   & lt;/dependency>   & lt; !——fastjson依赖添加——比;   & lt; dependency>   & lt; groupId> com.alibaba   & lt; artifactId> fastjson   & lt; version> 1.2.31   & lt;/dependency>   & lt;/dependencies>之前      

2,公共的结果类封装:
  

  

这里简单封装,实际根据自己业务需求去封装。

        @ getter   @ setter   公开课ApiResult {//响应业务状态   私人整数状态;//响应消息   私人字符串味精;//响应中的数据   私人对象数据;   公共静态ApiResult构建(整数地位,字符串味精、对象数据){   返回新ApiResult(地位、味精、数据);   }   公共静态ApiResult ok(对象数据){   返回新ApiResult(数据);   }   公共静态ApiResult好(){   返回新ApiResult(空);   }   公共ApiResult () {}   公共静态ApiResult构建(整数地位,字符串味精){   返回新ApiResult(地位、味精、空);   }   公共ApiResult(整数地位,字符串味精、对象数据){   这一点。状态=状态;   这一点。味精=味精;   这一点。数据=https://www.yisu.com/zixun/data;   }   公共ApiResult(对象数据){   这一点。状态=200;   这一点。味精=癘K”;   这一点。数据=https://www.yisu.com/zixun/data;   }   }      

3,添加全局异常处理类(在入口函数下的包中新建):

     /* *   *全局异常处理处理程序   * @ControllerAdvice配置控制器通知   *注释属性:指定我们需要拦截的注解,一个或多个(多个加到大括号中,逗号分隔)   *///@RestControllerAdvice=@ResponseBody + @ControllerAdvice   @RestControllerAdvice(注释={RestController.class})   @Slf4j   公开课GlobalExceptionHandler {/* *   *默认统一异常处理方法   * @ExceptionHandler注解用来配置需要拦截的异常类型,也可以是自定义异常   */@ExceptionHandler (Exception.class)//此处可以指定返回的状态码和返回结果说明//@ResponseStatus(原因=袄狻?价值=https://www.yisu.com/zixun/HttpStatus.BAD_REQUEST)   公共对象runtimeExceptionHandler(异常e) {//打印异常信息到控制台   e.printStackTrace ();   日志。错误(“请求出现异常,异常信息为:{}”,e.getMessage ());//使用公共的结果类封装返回结果,这里我指定状态码为400   ApiResult返回。构建(400年,e.getMessage ());   }   }      

4,异常测试类:

     /* *   *异常处理测试控制器   */@RestController   @Slf4j   公开课ExceptionController {   @RequestMapping (value=" https://www.yisu.com/exception/{号码}”)   公共ApiResult异常(@PathVariable int数){   int res=10/数量;   log.info(“在在在在在结果数量为:{}”,res);   返回ApiResult.ok ();   }   }

springboot全局异常处理代码实例