首页>代码>springboot集成rabbitmq生产者消费者模型>/springboot-rabbitmq/src/main/java/com/simon/springbootrabbitmq/config/ApiResult.java
package com.simon.springbootrabbitmq.config;

import com.simon.springbootrabbitmq.ApiErrorCode;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Optional;

/**
 * @author xxx
 */
@Data
@Accessors(chain = true)
public class ApiResult<T> implements Serializable {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    /**
     * 业务错误码
     */
    private long code;
    /**
     * 结果集
     */
    private T data;
    /**
     * 描述
     */
    private String msg;

    /**
     * 响应时间
     * 觉得格式不好看,自己处理一下
     */
    private String responseTime = LocalDateTime.now().toString();

    public ApiResult() {
        // to do nothing
    }

    public ApiResult(ApiErrorCode errorCode) {
        errorCode = Optional.ofNullable(errorCode).orElse(ApiErrorCode.FAILED);
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
    }

    public static <T> ApiResult<T> ok(T data) {
        ApiErrorCode aec = ApiErrorCode.SUCCESS;
        if (data instanceof Boolean && Boolean.FALSE.equals(data)) {
            aec = ApiErrorCode.FAILED;
        }
        return restResult(data, aec);
    }

    public static <T> ApiResult<T> failed(String msg) {
        return restResult(null, ApiErrorCode.FAILED.getCode(), msg);
    }

    public static <T> ApiResult<T> failed(ApiErrorCode errorCode) {
        return restResult(null, errorCode);
    }

    public static <T> ApiResult<T> restResult(T data, ApiErrorCode errorCode) {
        return restResult(data, errorCode.getCode(), errorCode.getMsg());
    }

    private static <T> ApiResult<T> restResult(T data, long code, String msg) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
最近下载更多
最代码官方  LV168 2023年2月26日
最近浏览更多
xp95323  LV15 6月10日
haitianjiaoyu0  LV8 5月27日
qinzhifang  LV1 3月15日
笛卡尔积  LV6 2023年9月25日
duon12  LV1 2023年9月25日
gaoxin222  LV14 2023年6月16日
1529860026  LV24 2023年6月1日
3054069007  LV1 2023年5月29日
陆程江  LV2 2023年5月29日
北。  LV6 2023年4月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友