定义全局异常封装类

发布时间:2023-05-23 09:31:49

通用返回对象定义Baseresponsensensensesponse

  • 这主要定义了一些返回到前端的数据
  • code:响应码
  • data:数据
  • message:状态码信息
  • description:状态码描述(详情)
  • 根据输入参数或需求,定义一些重载结构方法
  • 代码:
@Datapublic class BaseResponse<T> {    private int code;    private T data;    private String message;    private String description;    public BaseResponse(int code, T data, String message,String description) {        this.code = code;        this.data = data;        this.message = message;        this.description = description;    }    public BaseResponse(int code, T data, String message) {        this(code,data,message,"");    }    public BaseResponse(int code, T data) {        this(code,data,"","");    }    public BaseResponse(ErrorCode errorCode) {        this(errorCode.getCode(),null,errorCode.getMessage(),errorCode.getDescription());    }}
Resultutils定义

功能:一层包装Baseresponse,可以直接用工具类返回,让工具类帮助我们创建Beseresponse的实例对象

  • 定义了两种方法
  • 成功
public static <T>BaseResponse<T> success(T data) {    return new BaseResponse<>(0,data,"ok");}
  • 失败取决于以下整体异常处理
封装全局异常处理定义业务错误状态码
  • 创建一个定义我们业务错误信息的枚举类
  • 其中包括 code、message、description,因为我们不需要修改这些属性,并将其定义为final
/*** 状态码*/ private final int code;/*** 状态码信息*// private final String message;/*** 状态码描述(详情)*/ private final String description;
  • 定义构造器
ErrorCode(int code, String message, String description) {    this.code = code;    this.message = message;    this.description = description;}
  • 在某些业务中定义一些异常信息
SUCCESS(0,"ok","PARAMS_ERROR(40000,"请求参数错误","NULL_ERROR(40001,"请求参数为空","NOT_LOGIN(40100,"未登录","NO_AUTH(40101,"无权限","SYSTEM_ERROR(50000,"系统异常",";
  • 提供getter方法
  • 在我们的Resultutils中定义失败的方法,并根据不同的业务需求或参数重载
/**     * 失败     * @param errorCode     * @return     * @param <T>     */    public static <T>BaseResponse<T> error(ErrorCode errorCode) {        return new BaseResponse<>(errorCode);    }    public static <T>BaseResponse<T> error(ErrorCode errorCode,String description) {        return new BaseResponse<>(errorCode.getCode(),null,errorCode.getMessage(),description);    }    public static <T>BaseResponse<T> error(ErrorCode errorCode,String message, String description) {        return new BaseResponse<>(errorCode.getCode(),null,message,description);    }    public static <T>BaseResponse<T> error(int code,String message, String description) {        return new BaseResponse<>(code,null,message,description);    }
定义业务异常类别

作用:

  1. 与Java的异常类相比,支持更多的字段
  2. 自定义构造函数,更灵活 / 快速设置字段
public class BusinessException extends RuntimeException{    private final int code;    private final String description;    public BusinessException(int code, String message, String description) {        super(message);        this.code = code;        this.description = description;    }    public BusinessException(ErrorCode errorCode) {        super(errorCode.getMessage());        this.code = errorCode.getCode();        this.description = errorCode.getDescription();    }    public BusinessException(ErrorCode errorCode,String description) {        super(errorCode.getMessage());        this.code = errorCode.getCode();        this.description = description;    }    public int getCode() {        return code;    }    public String getDescription() {        return description;    }}
  • 好处:可以在controller层调用,也可以在业务层使用
包装全局处理器

作用:

  1. 捕获代码中的所有异常,内部消化,使前端得到更详细的业务错误 / 信息
  2. 同时,屏蔽项目框架本身的异常(不暴露服务器内部状态)
  3. 集中处理,如记录日志

实现:

使用Spring AOP思想在调用方法前后进行额外处理

@Restcontroleradvice注释

@RestcontrollerAdvice的作用范围是:所有在单个项目中使用@RequestMapping(比如@requestmaping底层使用@requestmaping注释也支持),属于@restcontcolleradvice,已经很清楚了。@RestcontrollerAdvice这个注释该怎么办?好像根据他的名字,我们只能看到这是一个应用于Controller层的截面注释,其他的都看不见。似乎根据他的名字,我们只能看到这是一个应用于Controller层的截面注释,其他的看不见。事实上,注释需要与其他注释一起使用才能有意义。单独使用注释是没有意义的。让我们介绍一下注释可以与哪些注释匹配合使用

@Exceptiondler

@RestControllerAdvice+@Exceptionhandler两种注释的组合被用作项目的全局异常处理。一旦项目中出现异常,@exceptionhandvice注释类中使用的@restcontioleradvice注释类中使用的@exceptionhandler注释方法将在这里处理全局异常。将异常信息输出到指定位置。并归置所有错误信息,以下是示例代码:

@RestControllerAdvice@Slf4jpublic class GlobalExceptionHandler {    /**     * 自定义异常     * @param e     * @return     */    @ExceptionHandler(BusinessException.class)    public BaseResponse businessException(BusinessException e) {        log.info("businessException" + e.getMessage(),e);        return ResultUtils.error(e.getCode(),e.getMessage(),e.getDescription());    }    /**     * 系统内异常     * @param e     * @return     */    @ExceptionHandler(RuntimeException.class)    public BaseResponse businessException(RuntimeException e) {        log.info("runtimeException",e);        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,e.getMessage(),";    }}
全局异常处理器的优点
  1. 前端不需要知道我们在哪里报错了
  2. 当我们的后端报错时,我们不会将系统本身的报错信息返回到前端(哪个类别、哪个方法、哪个错误)
  3. 提高安全性

上一篇 Redis开发规范——键值设计
下一篇 机器学习

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: Java教程Java基础Java编程技巧面试题Java面试题