4

SpringBoot定义优雅全局统一Restful API 响应框架四 - 程序员三时

 1 year ago
source link: https://www.cnblogs.com/kenx/p/17387625.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

SpringBoot定义优雅全局统一Restful API 响应框架四

如果没有看前面几篇文章请先看前面几篇

SpringBoot定义优雅全局统一Restful API 响应框架

SpringBoot定义优雅全局统一Restful API 响应框架二

SpringBoot定义优雅全局统一Restful API 响应框架三

目前我们好像似乎解决所有问题,达到了我们理想的效果如下

202305031647673.png

但是在业务错误返回时候不太理想如下

202305091956951.png

没有必要返回 reuqesterrorMsg

还有如果业务非常复杂的时候,会不会出现错误码分配使用混乱和重复问题这个问题应该如何避免和解决呢?

这个时候我想到 使用业务错误常量来代替错误码,这样更加见字识意, 进一步抽象错误常量公共接口模块

package cn.soboys.springbootrestfulapi.common.error;

import java.util.List;

/**
 * @author 公众号 程序员三时
 * @version 1.0
 * @date 2023/5/9 20:14
 * @webSite https://github.com/coder-amiao
 * 定义错误常量 代替错误码,避免业务复杂错误码分配重复等问题
 */
public interface CommonErrorConstant {
    /**
     * 公共错误码定义
     */
    public static final String InvalidRequest = "InvalidRequest";
    public static final String InvalidArgument = "InvalidArgument";
    public static final String NotFound = "NotFound";
    public static final String UnknownError = "UnknownError";
    public static final String OK = "OK";
    public static final String FAIL = "FAIL";



    /**
     * 其他自定义业务错误码
     */



}

通用错误码

package cn.soboys.springbootrestfulapi.common.error;

import cn.soboys.springbootrestfulapi.common.resp.ResultCode;

/**
 * @author 公众号 程序员三时
 * @version 1.0
 * @date 2023/5/2 21:36
 * @webSite https://github.com/coder-amiao
 */
public enum CommonErrorCode implements ResultCode {

    /**
     * 错误请求
     */
    INVALID_REQUEST(false, CommonErrorConstant.InvalidRequest, "Invalid request, for reason: "),
    /**
     * 参数验证错误
     */
    INVALID_ARGUMENT(false, CommonErrorConstant.InvalidArgument, "Validation failed for argument "),
    /**
     * 未找到资源
     */
    NOT_FOUND(false, CommonErrorConstant.NotFound, "Resource  not found."),
    /**
     * 未知错误
     */
    UNKNOWN_ERROR(false, CommonErrorConstant.UnknownError, "Unknown server internal error.");


    CommonErrorCode(Boolean success, String code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;

    }

    /**
     * 响应是否成功
     */
    private Boolean success;
    /**
     * 响应状态码
     */
    private String code;
    /**
     * 响应信息
     */
    private String message;


    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public boolean getSuccess() {
        return success;
    }


}

202305092219242.png
  1. 调整后的业务异常

    202305092215329.png
  2. 调整未知错误异常

202305092306275.png

对应代码已经上传更新github

202305092257364.png

在留一下一个思考问题,如果接口错误需要国际化应该如何实现?

如果你觉得对您有帮助关注公众号,程序员三时 我会给大家不定期分享热门技术,做开源项目,技术交流,面试,学习,分享自己入行多年一些感受和经验


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK