关于业务统一返回处理优化方式

统一返回格式

package com.giser.mp.common;

import org.apache.commons.lang3.StringUtils;

import io.swagger.v3.oas.annotations.media.Schema;

/**
 * 通用返回结果信息
 */

public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;

    @Schema(name = "响应状态码")
    private int code;

    @Schema(name = "提示信息")
    private String msg;

    @Schema(name = "返回数据")
    private T data;

    @Schema(name = "接口返回状态")
    private String status;

    @Schema(name = "操作成功状态")
    private boolean success;

    @Schema(name = "时间戳")
    private long timestamp;

    public static <T> Result<T> success(String message) {
        Result<T> bean = success();
        bean.setMsg(StringUtils.isEmpty(message) ? "操作成功" : message);
        return bean;
    }

    public static <T> Result<T> success(T content) {
        Result<T> bean = success();
        bean.setData(content);
        return bean;
    }

    public static <T> Result<T> success(String message,T content) {
        Result<T> bean = success();
        bean.setMsg(StringUtils.isEmpty(message) ? "操作成功" : message);
        bean.setData(content);
        return bean;
    }

    public static <T> Result<T> success() {
    	Result<T> builder = builder();
    	return builder.setCode(1)
				    	.setStatus("success")
				    	.setMsg("操作成功")
				    	.setSuccess(true)
				    	.setTimestamp(System.currentTimeMillis());
    }

    public static <T> Result<T> fail(String message) {
        Result<T> bean = fail();
        bean.setMsg(StringUtils.isEmpty(message) ? "操作失败" : message);
        return bean;
    }

    public static <T> Result<T> fail(T content) {
        Result<T> bean = fail();
        bean.setData(content);
        return bean;
    }

    public static <T> Result<T> fail(String message,T content) {
        Result<T> bean = fail();
        bean.setMsg(StringUtils.isEmpty(message) ? "操作失败" : message);
        bean.setData(content);
        return bean;
    }

    public static <T> Result<T> fail() {
    	Result<T> builder = builder();
    	return builder.setCode(0)
				    	.setStatus("error")
				    	.setMsg("操作失败")
				    	.setSuccess(false)
				    	.setTimestamp(System.currentTimeMillis());
    }

    private Result() {}
    private static <T> Result<T> builder(){
    	return new Result<T>();
    }

	public int getCode() {
		return code;
	}

	public Result<T> setCode(int code) {
		this.code = code;
		return this;
	}

	public String getMsg() {
		return msg;
	}

	public Result<T> setMsg(String msg) {
		this.msg = msg;
		return this;
	}

	public T getData() {
		return data;
	}

	public Result<T> setData(T data) {
		this.data = data;
		return this;
	}

	public String getStatus() {
		return status;
	}

	public Result<T> setStatus(String status) {
		this.status = status;
		return this;
	}

	public boolean isSuccess() {
		return success;
	}

	public Result<T> setSuccess(boolean success) {
		this.success = success;
		return this;
	}

    public long getTimestamp() {
        return timestamp;
    }

    public Result<T> setTimestamp(long timestamp) {
        this.timestamp = timestamp;
        return this;
    }
    
}

你可能感兴趣的:(java)