PHP笔记网

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2021-05-30 23:18:55  修改时间:2024-11-20 12:32:14  分类:07.Java基础  编辑

Result类功能:封装需要向前端传递的数据。

Result类的内部构造:在内部封装三个元素,code,msg,T data;和两个构造方法,一个用来传递成功信息,另一个用来传递失败信息。

import org.apache.commons.lang.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Result<T> extends BaseDomain implements Serializable {
    private String code;
    private String msg;
    private T data;

    private Result() {
        this.code = UnifyStrErrorCode.SUCCESS.getCode();
        this.msg = UnifyStrErrorCode.SUCCESS.getMsg();
    }

    private Result(T data) {
        this.code = UnifyStrErrorCode.SUCCESS.getCode();
        this.msg = UnifyStrErrorCode.SUCCESS.getMsg();
        this.setData(data);
    }

    private Result(String code, String msg) {
        this.code = UnifyStrErrorCode.SUCCESS.getCode();
        this.msg = UnifyStrErrorCode.SUCCESS.getMsg();
        this.setCode(code);
        this.setMsg(msg);
    }

    private Result(String code, String msg, T data) {
        this.code = UnifyStrErrorCode.SUCCESS.getCode();
        this.msg = UnifyStrErrorCode.SUCCESS.getMsg();
        this.setCode(code);
        this.setMsg(msg);
        this.setData(data);
    }

    public Result<T> setError(String code, String msg) {
        this.setCode(code);
        this.setMsg(msg);
        return this;
    }

    public Result<T> setError(UnifyStrErrorCode errorCode) {
        this.setCode(errorCode.getCode());
        this.setMsg(errorCode.getMsg());
        return this;
    }

    public boolean isSuccess() {
        return StringUtils.equals(this.getCode(), UnifyStrErrorCode.SUCCESS.getCode());
    }

    public static Result instance() {
        return new Result();
    }

    public static <T> Result instance(T data) {
        return new Result(data);
    }

    public static <T> Result instance(String code, String msg) {
        return new Result(code, msg);
    }

    public static <T> Result instance(String code, String msg, T data) {
        return new Result(code, msg, data);
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Map<String,Object> toJsonMap(){
        Map<String,Object> map = new HashMap<>();
        map.put("data",this.data);
        map.put("msg",this.msg);
        map.put("code",this.code);
        return  map;
    }

}

 

import org.apache.commons.lang.builder.ToStringBuilder;

import java.io.Serializable;

 
public class BaseDomain implements Serializable {
    public BaseDomain() {
    }

    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

 

摘自:Java的Result类

 

参考:

[荐]数据流动之使用泛型类Result<T>向前端传递数据

Spring Boot定义统一返回Result对象

Java工具类Result<T>