不处理异常
当我们使用SpringBoot开发程序,不进行任何的异常处理,那么在客户端就会报 500 的错误码,控制台(日志)打印错误信息。具体如下:
模拟错误:
发起请求:http://localhost:9201/user/info/1
前台报错:
后台打印日志的详细信息:
手工处理异常
在 Controller 的请求处理方法中手动的使用 try … catch 块的方式捕捉异常,当捕捉到特定的异常时,返回特定的逻辑视图名。但这种方式很繁琐,需要写大量的 catch 块。最大的缺点:异常的处理与业务代码耦合,一旦修改了异常处理的方式,就必须修改大量代码
@GetMapping("/hello")public String hello() throws Exception{try {....}catch (异常1 e) {return 结果1}catch (异常2 e) {return 结果2}return 结果}
@ExceptionHandler 注解处理异常
@ExceptionHandler 注解标注在方法上,其注解的 value 属性可以指定处理异常的类型(即捕获处理哪些异常)
基本使用
结果
优缺点和解决方法
优点:解决了异常处理的代码与业务逻辑代码耦合问题
缺点:每个Controller 类上都需要编写 标有@ExceptionHandle 注解的代码,出现大量的冗余
解决方法:编写 BaseController 类,把所有的 @ExceptionHandle 注解方法都放在 BaseController 类中,然后让其他Controller类继承即可
全局统一的异常处理
使用 @ExceptionHandler + @ControllerAdvice(或 @RestControllerAdvice)的方式全局统一处理异常
创建模块
在 ruoyi-common 模块下,创建新模块 ruoyi-common-security,其pom.xml 如下:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>ruoyi-common</artifactId><groupId>com.ruoyi</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ruoyi-common-security</artifactId></project>
版本声明
在 ruoyi 模块中声明版本
<dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-security</artifactId><version>${ruoyi.version}</version></dependency>
引入依赖
在 ruoyi-common-security 模块中引入3个依赖分别是 spring-boot-starter-web、lombok、ruoyi-common-core
spring-boot-starter-web 引入的作用是:@RestControllerAdvice 注解需要
lombok 引入的作用:@Slf4j 注解需要
ruoyi-common-core 引入的作用:该模块中定义了基础类,比如说,自定义异常,返回结果(R和 AjaxResult 类)
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>ruoyi-common</artifactId><groupId>com.ruoyi</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ruoyi-common-security</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-core</artifactId></dependency></dependencies></project>
全局异常处理类
package com.ruoyi.common.security.handler;import com.ruoyi.common.core.domain.R;import lombok.extern.slf4j.Slf4j;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RestControllerAdvice;@RestControllerAdvice@Slf4jpublic class GlobalExceptionHandler {@ExceptionHandler(value = ArithmeticException.class)public R errorHandler(Exception e) {log.error(e.getMessage());return R.fail(e.getMessage());}}
全局异常处理类来处理异常
在 ruoyi-modules-system 模块中,引入依赖 ruoyi-common-security, 该模块中含有标记有 @RestControllerAdvice 或 @ControllerAdvice 注解的类,当处理异常时,会去这个类中寻找有没有合适的方法处理异常
<dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common-security</artifactId></dependency>
运行结果:

