Utils-返回工具类ResultVo JSONResult

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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值