jiangping
2024-01-16 c2f1aac8acca57f4c21f6fe6718101b01805bc72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.jzq.common;
 
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
 
import java.beans.Transient;
import java.io.Serializable;
import java.util.List;
 
/**
 * 返回对象
 * @author luopeng
 *
 */
@Data
public class ResultInfo<T> implements Serializable {
 
    private static final long serialVersionUID = -1L;
 
    private boolean success; // 操作是否成功
    private String msg; // 操作失败的原因
 
    private String resultCode;//返回码
    
    private T data;//返回对象
    
    /**异常对象不作序列化传输*/
    private transient Exception exception;//异常对象
    
    public ResultInfo(){}
 
    public static <T> ResultInfo<T> create(Class<T> cls){
        return new ResultInfo<T>();
    }
    
    public static ResultInfo<Void> create(){
        return new ResultInfo<Void>();
    }
    
    public static ResultInfo<Void> createFail(Exception e){
        ResultInfo<Void> result = new ResultInfo<Void>();
        result.fail(e);
        return result;
    }
    
    public static ResultInfo<Void> createFail(String msg,Exception e){
        ResultInfo<Void> result = new ResultInfo<Void>();
        result.fail(msg,e);
        return result;
    }
    
    public ResultInfo<T> success(){
        this.success = true;
        return this;
    }
    
    public ResultInfo<T> success(T data){
        this.success = true;
        this.data = data;
        return this;
    }
    
    public ResultInfo<T> fail(){
        this.success = false;
        return this;
    }
    
    public ResultInfo<T> fail(String msg){
        this.success = false;
        this.msg = msg;
        return this;
    }
    
    public ResultInfo<T> fail(Exception e){
        this.success = false;
        if(e != null){
            this.exception = e;
            this.msg = e.getMessage();
        }
        return this;
    }
    
    public ResultInfo<T> fail(String msg,Exception e){
        this.success = false;
        this.msg = msg;
        if(e != null){
            this.exception = e;
        }
        return this;
    }
 
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}