com.zcj.web.dto.ApiResult Maven / Gradle / Ivy
package com.zcj.web.dto;
import com.google.gson.Gson;
import com.zcj.util.UtilString;
import com.zcj.util.json.gson.GsonInstance;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
/**
* 用于REST风格API的返回格式
*
* @author [email protected]
* @since 2019/1/2
*/
public class ApiResult implements Serializable, ApiResultable {
private static final long serialVersionUID = 3715411520951675028L;
@ApiModelProperty(value = "是否成功", example = "true", required = true)
private Boolean success;
@ApiModelProperty(value = "返回标识码(0:成功;1:失败;2:token错误;3:签名错误)", example = "0", required = true)
private Integer code;
@ApiModelProperty("内容")
private T data;
@ApiModelProperty("描述")
private String message;
@ApiModelProperty(value = "数据总量", example = "0")
private Integer total;
private static final int CODE_SUCCESS = 0;// 成功
private static final int CODE_ERROR = 1;// 失败
private static final int CODE_ERROR_TOKEN = 2;// token错误
private static final int CODE_ERROR_SIGN = 3;// 签名错误
private static final String MESSAGE_SUCCESS = "操作成功";
private static final String MESSAGE_ERROR_PARAM = "参数错误";
public static final Gson GSON_DT = GsonInstance.GSON_DEFAULT;
@Override
public boolean success() {
return CODE_SUCCESS == this.getCode();
}
@Override
public String message() {
return this.getMessage();
}
@Override
public String toJson() {
return GSON_DT.toJson(this);
}
@Override
public ApiResult buildError(String errorInfo) {
return new ApiResult<>(CODE_ERROR, errorInfo, null, null);
}
@Override
public ApiResult buildSignError() {
return new ApiResult<>(CODE_ERROR_SIGN, MESSAGE_ERROR_SIGN, null, null);
}
@Override
public ApiResult buildTokenError() {
return new ApiResult<>(CODE_ERROR_TOKEN, MESSAGE_ERROR_TOKEN, null, null);
}
@Override
public ApiResultable buildTokenError(String errorInfo) {
return new ApiResult<>(CODE_ERROR_TOKEN, UtilString.empty2String(errorInfo, MESSAGE_ERROR_TOKEN), null, null);
}
public static ApiResult fromServiceResult(ServiceResult serviceResult) {
if (serviceResult == null) {
return null;
}
ApiResult result = new ApiResult();
result.setData(serviceResult.getData());
result.setMessage(serviceResult.getMsg());
result.setTotal(serviceResult.getCount());
result.setCode(serviceResult.success() ? CODE_SUCCESS : CODE_ERROR);
result.setSuccess(serviceResult.success());
return result;
}
public static ApiResult initSuccess(T data) {
return new ApiResult<>(CODE_SUCCESS, MESSAGE_SUCCESS, data, null);
}
public static ApiResult initSuccess(T data, String message) {
return new ApiResult<>(CODE_SUCCESS, message, data, null);
}
public static ApiResult initSuccessPage(T data, Integer total) {
return new ApiResult<>(CODE_SUCCESS, MESSAGE_SUCCESS, data, total);
}
public static ApiResult initError(String message) {
return new ApiResult<>(CODE_ERROR, message, null, null);
}
public static String initSuccessJson(T d) {
return GSON_DT.toJson(initSuccess(d));
}
public static String initSuccessPageJson(T data, Integer total) {
return GSON_DT.toJson(initSuccessPage(data, total));
}
public static String initErrorJson(String message) {
return GSON_DT.toJson(initError(message));
}
public static String initErrorParamJson() {
return GSON_DT.toJson(initError(MESSAGE_ERROR_PARAM));
}
public ApiResult() {
}
private ApiResult(Integer code, String message, T data, Integer total) {
this.code = code;
this.message = message;
this.data = data;
this.total = total;
this.success = success();
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy