All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.boot.common.dto.Result Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.boot.common.dto;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 请求返回结果
 * @author wangcheng
 */
@Data
@Accessors(chain = true)
public class Result implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 是否成功
     */
    private Boolean success;

    /**
     * 消息
     */
    private String msg;

    /**
     * 返回体
     */
    private T body;

    /**
     * 错误编码
     */
    private Integer errorCode;

    /**
     * 返回成功
     * @param body 内容
     * @param  body类型
     * @return result
     */
    public static  Result success(S body) {
        return new Result().setSuccess(true)
                .setMsg("SUCCESS").setBody(body);
    }

    /**
     * 返回成功
     * @return result
     */
    public static  Result success() {
        return Result.success(null);
    }

    /**
     * 返回失败
     * @param msg 消息内容
     * @return result
     */
    public static  Result fail(String msg) {
        return new Result().setSuccess(false).setMsg(msg);
    }

    /**
     * 返回失败
     * @param msg 消息内容
     * @param errorCode 错误编码
     * @return result
     */
    public static  Result fail(String msg, Integer errorCode) {
        return new Result().setSuccess(false).setMsg(msg).setErrorCode(errorCode);
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

}