Java后端数据返回通用类

第一种(简单定义)

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
97
98
99
100
101
import lombok.Data;
import java.util.HashMap;
import java.util.Map;

/**
* 结果数据返回通用类
* @author : 其然乐衣Letitbe
* @date : 2022/7/20
*/

@Data
public class JsonResult<T> {
/**
* 编码:200成功,500和其它数字为失败或其他异常情况
**/
private Integer code;
/**
* 返回描述信息
**/
private String msg;
/**
* 信息返回数据
**/
private T data;
/**
* 动态数据
**/

private Map map = new HashMap();

public JsonResult() {

}

public JsonResult(Integer code) {
this.code = code;
}

public JsonResult(Integer code, String msg) {
this.code = code;
this.msg = msg;
}

public static <T> JsonResult<T> success(String msg, T object) {
JsonResult<T> result = new JsonResult<T>();
result.msg = msg;
result.data = object;
result.code = 200;
return result;
}

public static <T> JsonResult<T> success(String msg, Integer code) {
JsonResult result = new JsonResult();
result.msg = msg;
result.code = code;
return result;
}

public static <T> JsonResult<T> success( Integer code) {
JsonResult result = new JsonResult();
result.code = code;
return result;
}

public static <T> JsonResult<T> success(String msg) {
JsonResult<T> result = new JsonResult<T>();
result.msg = msg;
result.code = 200;
return result;
}

public static <T> JsonResult<T> success() {
JsonResult<T> result = new JsonResult<T>();
result.code = 200;
return result;
}

public static <T> JsonResult<T> error(String msg, Integer code) {
JsonResult result = new JsonResult();
result.msg = msg;
result.code = code;
return result;
}

public static <T> JsonResult<T> error(Integer code) {
JsonResult result = new JsonResult();
result.code = code;
return result;
}

public static <T> JsonResult<T> error() {
JsonResult result = new JsonResult();
result.code = 500;
return result;
}

public JsonResult<T> add(String key, Object value) {
this.map.put(key, value);
return this;
}
}

第二种

导入依赖

1
2
3
4
5
6
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.13</version>
<scope>compile</scope>
</dependency>

编写结果数据通用类

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
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
* todo JsonInclude 注解忽略 null值 看业务需求添加
*/
@ApiModel("分页响应参数")
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
@Data
public class JsonResult<T> implements Serializable {
private static final long serialVersionUID = 1L;

@ApiModelProperty(value = "结果状态", example = "200")
private Integer code = 200;
@ApiModelProperty(value = "结果数据")
private T data;
@ApiModelProperty(value = "结果描述", example = "成功")
private String message = "ok";

public JsonResult() {
}

public JsonResult(Integer code) {
this();
this.code = code;
}

public JsonResult(Integer code, T data) {
this(code);
this.data = data;
}

public JsonResult(Integer code, String message) {
this(code);
this.message = message;
}

public JsonResult(Integer code, String message, T data) {
this(code, message);
this.data = data;
}

public static <T> JsonResult<T> success() {
return new JsonResult(200);
}

public static <T> JsonResult<T> success(T data) {
return new JsonResult(200, data);
}

public static <T> JsonResult<T> success(String message, T data) {
return new JsonResult(200, message, data);
}

public static <T> JsonResult<T> success(Integer code, String message, T data) {
return new JsonResult(code, message, data);
}

public static JsonResult<String> error() {
return new JsonResult(500);
}

public static <T> JsonResult<T> error(String message) {
return new JsonResult(500, message);
}

public static <T> JsonResult<T> error(Integer code, String message) {
return new JsonResult(code, message);
}

public static <T> JsonResult<T> error(Integer code, String message, T data) {
return new JsonResult(code, message, data);
}


}