cn.cliveyuan.robin.base.common.WebApiResponse Maven / Gradle / Ivy
package cn.cliveyuan.robin.base.common;
import java.io.Serializable;
import java.util.Objects;
/**
* 统一接口响应对象
*
* @author Clive Yuan
* @date 2020/12/24
*/
public class WebApiResponse implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 编码
*/
private int code = ApiErrorCode.FAIL.getCode();
/**
* 消息
*/
private String msg;
/**
* 数据
*/
private T data;
public WebApiResponse() {
}
public static WebApiResponse success(T data) {
WebApiResponse webApiResponse = new WebApiResponse<>();
webApiResponse.setCode(0);
webApiResponse.setMsg("success");
webApiResponse.setData(data);
return webApiResponse;
}
public static WebApiResponse fail(ApiErrorCode apiErrorCode) {
WebApiResponse webApiResponse = new WebApiResponse<>();
webApiResponse.setCode(apiErrorCode.getCode());
webApiResponse.setMsg(apiErrorCode.getMsg());
return webApiResponse;
}
public static WebApiResponse fail(ApiErrorCode apiErrorCode, T data) {
WebApiResponse webApiResponse = new WebApiResponse<>();
webApiResponse.setCode(apiErrorCode.getCode());
webApiResponse.setMsg(apiErrorCode.getMsg());
webApiResponse.setData(data);
return webApiResponse;
}
public static WebApiResponse fail(int code, String msg) {
WebApiResponse webApiResponse = new WebApiResponse<>();
webApiResponse.setCode(code);
webApiResponse.setMsg(msg);
return webApiResponse;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "ApiResponse{" +
"code=" + code +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WebApiResponse> that = (WebApiResponse>) o;
return code == that.code &&
Objects.equals(msg, that.msg) &&
Objects.equals(data, that.data);
}
@Override
public int hashCode() {
return Objects.hash(code, msg, data);
}
}