java项目中对于结果集的封装

众所周知,项目中对于返回结果集的封装是必不可少的,今天就记录一下本人在工作中关于结果集的那些事~

一、相关依赖项

    <dependencies>

        <!-- Fastjson. -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.68</version>
        </dependency>

        <!-- Lombok. -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
        </dependency>

        <!-- jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>

    </dependencies>

二、返回结果集实体 ActionResult<T>

/**
 * @Description: 异常信息处理实体
 * @Author: zhang
 * @Date: 2023/10/24 16:51
 **/
@Data
@NoArgsConstructor
@ApiModel
public class ActionResult<T> implements Serializable {

    @ApiModelProperty(name = "code", value = "状态码(0:成功,其他:失败)")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer code;

    @ApiModelProperty(name = "message",value = "状态说明")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String message;

    @ApiModelProperty(name = "timestamp",value = "时间戳")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private long timestamp;

    @ApiModelProperty(name = "data",value = "返回数据")
    @Setter(AccessLevel.PROTECTED)
    private T data;

  //  @ApiModelProperty(value = "链路追踪id")
  //  private String traceId= TraceContext.traceId();

    @JsonIgnore
    public boolean isOk() {
        return code == null || code == 0;
    }

    public static <T> ActionResult<T> ok() {
        ActionResult<T> ret = new ActionResult<T>();
        ret.code = ErrorCodes.OK;
        ret.message = "success";
        return ret;
    }
    public static <T> ActionResult<T> ok(Integer code) {
        ActionResult<T> ret = new ActionResult<T>();
        ret.code = code;
        ret.message = "success";
        return ret;
    }

    public static <T> ActionResult<T> ok(T content) {
        ActionResult<T> ret = ok();
        ret.data = content;
        return ret;
    }

    public static <T> ActionResult<T> ok(T content, String message) {
        ActionResult<T> ret = ok();
        ret.data = content;
        ret.setMessage(message);
        return ret;
    }


    public static <T> ActionResult<T> ok(Integer code, String message) {
        ActionResult<T> ret = ok();
        ret.message = message;
        ret.code = code;
        return ret;
    }

    public static <T> ActionResult<T> ok(Integer code, T content) {
        ActionResult<T> ret = ok();
        ret.data = content;
        ret.code = code;
        return ret;
    }

    public static <T> ActionResult<T> ok(Integer code, T content, String message) {
        ActionResult<T> ret = ok();
        ret.data = content;
        ret.code = code;
        ret.setMessage(message);
        return ret;
    }

    public static <T> ActionResult<T> notFound() {
        return error(ErrorCodes.NOT_FOUND,"没有找到记录");
    }

    public static <T> ActionResult<T> notFound(String message) {
        return error(ErrorCodes.NOT_FOUND , message);
    }

    public static <T> ActionResult<T> illegal() {
        return ActionResult.error(ErrorCodes.ILLEGAL_OPERATE,"非法的操作");
    }

    public static <T> ActionResult<T> error(int code, String message) {
        ActionResult<T> ret = new ActionResult<T>();
        ret.code = code;
        ret.message = message;
        return ret;
    }

    public static <T> ActionResult<T> error(int code, String message, T content) {
        ActionResult<T> ret = new ActionResult<T>();
        ret.code = code;
        ret.message = message;
        ret.data = content;
        return ret;
    }

    public static <T> ActionResult<T> error(String message) {
        return error(ErrorCodes.BUSINESS, message);
    }


    public static <T> ActionResult<T> error() {
        return error(ErrorCodes.BUSINESS,"业务异常");
    }

    public static <T> ActionResult<T> error(ActionResult<?> result) {
        return error(result.getCode(), result.getMessage());
    }

    public long getTimestamp() {
        return System.currentTimeMillis();
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public T throwExIfError() {
        if (isOk()) {
            return data;
        }
        throw new ApplicationException(this);
    }

    public <NT> ActionResult<NT> newError() {
        return ActionResult.error(this);
    }

}

三、异常信息处理对象 ApplicationException

/**
 * @Description: 异常信息处理类
 * @Author: zhang
 * @Date: 2023/10/24 16:51
 **/
@Data
@Slf4j
@EqualsAndHashCode(callSuper = false)
public class ApplicationException extends RuntimeException {

    private static final long serialVersionUID = 2140756274765383719L;

    private ActionResult<?> result;

    public ApplicationException() {
        result = ActionResult.error();
    }

    public ApplicationException(String errmsg) {
        result = ActionResult.error(errmsg);
    }

    public ApplicationException(int errcode, String errmsg) {
        result = ActionResult.error(errcode, errmsg);
    }

    public <T> ApplicationException(int errcode, String errmsg, T data) {
        result = ActionResult.error(errcode, errmsg, data);
    }

    public ApplicationException(ActionResult<?> result) {
        this.result = result;
    }

    @Override
    public String getMessage() {
        return result.getMessage();
    }

    public ActionResult<?> getResult() {
        return result;
    }
}

四、异常信息编码(仅示例)

/**
 * @Description: 异常信息编码
 * @Author: zhang
 * @Date: 2023/10/24 16:51
 **/
public class ErrorCodes {

    public static final int SYS_UPGRADE = -2;//系统升级

    public static final int BUSY = -1; // 系统繁忙,此时请开发者稍候再试

    public static final int OK = 0;//请求成功

    /**
     * TOKEN相关
     */
    public static final int EXPIRED = 4000; //过期的
    public static final int EMPTY_TOKEN = 4002; //空令牌

    /*无效的**/
    public static final int INVALID_ARGUMENTS = 4100;//无效的参数
    /*缺少的*/
    public static final int MISSING_ARGUMENTS = 4200; //缺少参数

    /*重复的*/
    public static final int REPEAT = 4300; // 重复记录


    /*未找到的*/
    public static final int NOT_FOUND = 4400; //没有找到记录

    /*超时的*/
    public static final int TIMEOUT = 4500; //超时

    /*拒绝的*/
    public static final int DENIED_ACCESS = 4600; //拒绝访问
    public static final int DENIED_UNAUTHORIZED = 4601; //未授权:由于凭据无效,访问被拒绝

    /*非法的操作*/
    public static final int ILLEGAL_OPERATE = 4700; //非法的操作

    /*业务错误*/
    public static final int BUSINESS = 5000;//业务异常
    public static final int GET_ORDER_FAIL = 5001; //订单不存在
}

以上就是一整个返回结果集的闭环实现啦

欢迎学习交流~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值