package com.demo.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* explain 统一返回对象
*/
public class ResultVo<T> implements Serializable {
private static final long serialVersionUID = 1L;
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 是否成功
*/
private boolean success = true;
/**
* 需要返回到前台的数据
*/
private T data;
/**
* 消息
*/
private String msg = "成功";
/**
* 错误编码
*/
private int code;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return this.data;
}
public void setData(final T data) {
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public ResultVo(boolean success, String msg) {
this.success = success;
this.msg = msg;
}
public ResultVo(T data) {
this.data = data;
}
public ResultVo() {
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
public String toJSONString() throws JsonProcessingException {
Map obj = new HashMap<String,Object>();
if (this.isSuccess()) {
obj.put("data", getData() == null ? "" : getData());
obj.put("success", true);
} else {
Map errorObj = new HashMap<String,Object>();
errorObj.put("code", getCode());
errorObj.put("msg", getMsg());
obj.put("error", errorObj);
obj.put("success", false);
}
return MAPPER.writeValueAsString(obj);
}
public String toJSONStringWithOutNull() throws JsonProcessingException {
Map obj = new HashMap<String,Object>();
if (this.isSuccess()) {
obj.put("data", getData() == null ? "" : getData());
obj.put("success", true);
} else {
Map errorObj = new HashMap<String,Object>();
errorObj.put("code", getCode());
errorObj.put("msg", getMsg());
obj.put("error", errorObj);
obj.put("success", false);
}
return MAPPER.writeValueAsString(obj);
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof ResultVo)) {
return false;
} else {
ResultVo<?> other = (ResultVo) o;
if (!other.canEqual(this)) {
return false;
} else if (this.isSuccess() != other.isSuccess()) {
return false;
} else if (this.getCode() != other.getCode()) {
return false;
} else {
label40:
{
Object this$message = this.getMsg();
Object other$message = other.getMsg();
if (this$message == null) {
if (other$message == null) {
break label40;
}
} else if (this$message.equals(other$message)) {
break label40;
}
return false;
}
Object this$data = this.getData();
Object other$data = other.getData();
if (this$data == null) {
if (other$data != null) {
return false;
}
} else if (!this$data.equals(other$data)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof ResultVo;
}
public int hashCode() {
int result = 1;
result = result * 59 + (this.isSuccess() ? 79 : 97);
result = result * 59 + this.getCode();
Object $message = this.getMsg();
result = result * 59 + ($message == null ? 43 : $message.hashCode());
Object $data = this.getData();
result = result * 59 + ($data == null ? 43 : $data.hashCode());
return result;
}
}
Utils-返回工具类ResultVo JSONResult
于 2019-05-14 18:09:25 首次发布