package com.kidgrow.common.model;
|
|
import com.alibaba.fastjson.annotation.JSONField;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.google.common.collect.Maps;
|
import lombok.Data;
|
|
import java.io.Serializable;
|
import java.util.Map;
|
import java.util.ResourceBundle;
|
//import io.swagger.annotations.ApiModel;
|
//import io.swagger.annotations.ApiModelProperty;
|
|
/**
|
* 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
|
*
|
* @Description: <br>
|
* @Project: <br>
|
* @CreateDate: Created in 2020/2/4 13:41 <br>
|
* @Author: <a href="4345453@kidgrow.com">liuke</a>
|
*/
|
//@ApiModel(value = "响应结果")
|
@Data
|
public class ResultBody<T> implements Serializable {
|
private static final long serialVersionUID = -6190689122701100762L;
|
|
/**
|
* 响应编码
|
*/
|
// @ApiModelProperty(value = "响应编码:0-请求处理成功")
|
private int code = 0;
|
/**
|
* 提示消息
|
*/
|
// @ApiModelProperty(value = "提示消息")
|
private String msg;
|
|
/**
|
* 请求路径
|
*/
|
// @ApiModelProperty(value = "请求路径")
|
private String path;
|
|
/**
|
* 响应数据
|
*/
|
// @ApiModelProperty(value = "响应数据")
|
private T data;
|
|
/**
|
* http状态码
|
*/
|
private int httpStatus;
|
|
/**
|
* 附加数据
|
*/
|
// @ApiModelProperty(value = "附加数据")
|
private Map<String, Object> extra;
|
|
/**
|
* 响应时间
|
*/
|
// @ApiModelProperty(value = "响应时间")
|
private long timestamp = System.currentTimeMillis();
|
|
public ResultBody() {
|
super();
|
}
|
|
public ResultBody(int code,String msg,T data){
|
this.code=code;
|
this.msg=msg;
|
this.data=data;
|
}
|
|
public int getCode() {
|
return code;
|
}
|
|
public String getMsg() {
|
return msg;
|
}
|
|
public String getPath() {
|
return path;
|
}
|
|
public T getData() {
|
return data;
|
}
|
|
public Map<String, Object> getExtra() {
|
return extra;
|
}
|
|
public long getTimestamp() {
|
return timestamp;
|
}
|
|
@JSONField(serialize = false, deserialize = false)
|
@JsonIgnore
|
public int getHttpStatus() {
|
return httpStatus;
|
}
|
|
@JSONField(serialize = false, deserialize = false)
|
@JsonIgnore
|
public boolean isOk() {
|
return this.code == ErrorCode.OK.getCode();
|
}
|
|
|
public static ResultBody ok() {
|
return new ResultBody().code(ErrorCode.OK.getCode()).msg(ErrorCode.OK.getMessage());
|
}
|
|
|
public static ResultBody failed() {
|
return new ResultBody().code(ErrorCode.FAIL.getCode()).msg(ErrorCode.FAIL.getMessage());
|
}
|
|
public static ResultBody failed(String msg) {
|
return new ResultBody().code(ErrorCode.FAIL.getCode()).msg(msg);
|
}
|
|
public static ResultBody ok(int code,String msg) {
|
|
return new ResultBody(code,msg,null);
|
}
|
|
public static ResultBody failed(int code,String msg) {
|
|
return new ResultBody(code,msg,null);
|
}
|
|
|
public static <T> ResultBody<T> fail(int code, String msg) {
|
return new ResultBody<>(code, msg,null);
|
}
|
|
|
public ResultBody code(int code) {
|
this.code = code;
|
return this;
|
}
|
|
public ResultBody msg(String msg) {
|
this.msg = i18n(ErrorCode.getResultEnum(this.code).getMessage(), msg);
|
return this;
|
}
|
|
public ResultBody data(T data) {
|
this.data = data;
|
return this;
|
}
|
|
public ResultBody path(String path) {
|
this.path = path;
|
return this;
|
}
|
|
public ResultBody httpStatus(int httpStatus) {
|
this.httpStatus = httpStatus;
|
return this;
|
}
|
|
public ResultBody put(String key, Object value) {
|
if (this.extra == null) {
|
this.extra = Maps.newHashMap();
|
}
|
this.extra.put(key, value);
|
return this;
|
}
|
|
@Override
|
public String toString() {
|
return "ResultBody{" +
|
"code=" + code +
|
", msg='" + msg + '\'' +
|
", path='" + path + '\'' +
|
", data=" + data +
|
", httpStatus=" + httpStatus +
|
", extra=" + extra +
|
", timestamp=" + timestamp +
|
'}';
|
}
|
|
/**
|
* 错误信息配置
|
*/
|
@JSONField(serialize = false, deserialize = false)
|
@JsonIgnore
|
private static ResourceBundle resourceBundle = ResourceBundle.getBundle("error");
|
|
/**
|
* 提示信息国际化
|
*
|
* @param msg
|
* @param defaultMessage
|
* @return
|
*/
|
@JSONField(serialize = false, deserialize = false)
|
@JsonIgnore
|
private static String i18n(String msg, String defaultMessage) {
|
return resourceBundle.containsKey(msg)?resourceBundle.getString(msg):defaultMessage;
|
}
|
|
|
}
|