如何优雅的处理全局异常和返回结果

1,669 阅读3分钟

完整项目地址:micro-service-plus

系列文章入口:

前言

主要介绍@RestControllerAdvice捕捉全局异常,然后返回给前端显示

我们这边主要以ty-example为例进行说明

注:micro-service-plus里的微服务都会引用ty-common里的配置和依赖

核心文件介绍

ty-common/khcomomon/exception/BaseExceptionHandler

/*
* 全局异常处理
* */
//@RestControllerAdvice 捕捉当前微服务里的错误,并将错误信息返回前端
@RestControllerAdvice
public class BaseExceptionHandler {
    //方法参数传入移除监听,如果传入的方法和参数有问题,直接return回前端
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResResult MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        return  new ResResult().error(objectError.getDefaultMessage());
    }
    //微服务异常监听,如果微服务中产生Exception相关的异常(包括RunTimeException),直接return回前端
    @ExceptionHandler(value = Exception.class)
    public ResResult error(Exception e) {
        e.printStackTrace();
        return new ResResult().error(e.getMessage());
    }
}

在ty-example中的config/BaseConfig

/*
 * 总结:1.@Configuration 下的@ComponentScan回将包下带有@Component扫描变成配置类,
 * 而@SpringBootApplication扫描的只会变成普通类
 * */
@Configuration
@ComponentScan(basePackages = {"top.kuanghua.khcomomon"})
//mapper  scanning
@MapperScan(basePackages = {"top.kuanghua.tyexample.mapper"})
//feign scanning
@EnableFeignClients(basePackages = {"top.kuanghua.feign"})
public class BaseConfig {

}

@ComponentScan(basePackages = {"top.kuanghua.khcomomon"})

会将ty-common/khcomomon下的配置

1642144985021

全部都扫进来,包括全局异常处理配置(BaseExceptionHandler),mybitsPlusConfig基本配置(MybatisPlusConfig),swagger基本配置等

注:在每个微服务中都有BaseConfig文件,用于通用基本配置,,包括全局异常处理配置(BaseExceptionHandler),mybitsPlusConfig基本配置(MybatisPlusConfig),swagger基本配置等

现在已经将BaseExceptionHandler导入到项目了,那么如何更优雅的使用它呢?

我们看一段代码

ty-example/service/UploadService

        //检验文件内容
        BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
        if (bufferedImage == null) {
            log.info("文件不能为空:{}", originalFilename);
            throw new RuntimeException("文件不能为空");
        }

throw new RuntimeException("文件内容不合法"); 这段代码写在service中,但是BaseExceptionHandler将会进行捕捉直接返回前端,不用再controller层自己处理。所以以后有啥异常想直接返回告诉前端就用throw new RuntimeException。省了很多事情

体验地址

前端收到的内容如下:

{flag: false, code: 20001, msg: "文件不能为空", data: null}

因此你以后在service,不用写太多处理错误的逻辑,也不用写返回前端的代码逻辑,直接写一个throw new RuntimeException("文件不能为空"),就行了

通用返回类ResResult

ty-common\src\main\java\top\kuanghua\khcomomon\entity\ResResult.java

@Data
@AllArgsConstructor
public class ResResult<T> implements Serializable {
    private static final long serialVersionUID = 2344775856369318037L;
    private boolean flag;//是否成功
    private Integer code;//返回码
    private String msg;//返回消息
    private T data;//返回数据

    //成功返回可传入data
    public ResResult success(Object data) {
        this.flag = true;
        this.code = StatusCode.OK;
        this.data = (T) data;
        return this;
    }
    //成功返回
    public ResResult success() {
        this.flag = true;
        this.code = StatusCode.OK;
        this.msg = "操作成功!";
        return this;
    }
    //失败返回可传入msg
    public ResResult error(String msg) {
        this.flag = false;
        this.code = StatusCode.ERROR;
        this.msg = msg;
        return this;
    }
   //失败返回可传入msg和data
    public ResResult(String msg, Object data) {
        this.flag = true;
        this.code = StatusCode.OK;
        this.msg = msg;
        this.data = (T) data;
    }

    public ResResult(boolean flag, Integer code, String msg) {
        this.flag = flag;
        this.code = code;
        this.msg = msg;
    }

    public ResResult() {
        this.flag = true;
        this.code = StatusCode.OK;
        this.msg = "操作成功!";
    }

}

如何使用

//成功
@GetMapping("sendEmailText")
public ResResult sendEmailText() {
  return new ResResult().success("发送邮件成功");
}
//失败
return new ResResult().error(e.getMessage());