服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java封装统一的Result Model案例

Java封装统一的Result Model案例

2020-08-24 10:32试水流连 Java教程

这篇文章主要介绍了Java封装统一的Result Model案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

在开发过程中,有时候会需要使用错误码+错误信息的形式,来返回某些业务操作的错误结果信息,来代替效率较低的异常传递。

这样就需要封装一个统一的Result model作为返回值,代替直接返回数据等结果。

1.定义错误码 - 错误信息接口

错误码可能是String、Integer、Long 等类型,也可能是enum类型。因此这里使用泛型来代替,错误码类型,可以提高灵活性

。也可以通过枚举直接实现CodeMessage接口。

?
1
2
3
4
public interface CodeMessage<C>{
  C getCode();
  String getMessage();
}

2.实现Result Model

这里定义的是service层的Result。有时候在controller层只会作一些简单的参数校验,在service层会作进一步的校验,

如果controller中需要统一返回一个JsonResult可以将ServiceResult作一个简单的转换器进行转换。

?
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
public class ServiceResult<T, C> implements Serializable {
 
  public static final CodeMessage<String> SUCCESS = new DefaultMessage<>("00000000", "success");
 
  private T data;
  private CodeMessage<C> message;
  private boolean isSuccess;
 
  ServiceResult(T data, boolean isSuccess, CodeMessage<C> message) {
    this.data = data;
    this.message = message;
    this.isSuccess = isSuccess;
  }
 
  public T getData() {
    return data;
  }
 
  public CodeMessage<C> getCodeMessage() {
    return message;
  }
 
  public boolean isSuccess() {
    return isSuccess;
  }
 
  public static <D, C> ServiceResultBuilder<D, C> success() {
    ServiceResultBuilder<D, C> builder = builder();
    return builder.isSuccess(true);
  }
 
  public static <D, C> ServiceResultBuilder<D, C> success(CodeMessage<C> codeMessage) {
    ServiceResultBuilder<D, C> builder = builder();
    return builder.isSuccess(true).code(codeMessage.getCode()).message(codeMessage.getMessage());
  }
 
  public static <D> ServiceResult<D, String> success(D data) {
    ServiceResultBuilder<D, String> success = success(SUCCESS);
    return success.data(data).build();
  }
 
  public static <D, C> ServiceResult<D, C> error(CodeMessage<C> codeMessage) {
    ServiceResultBuilder<D, C> builder = builder();
    return builder.isSuccess(false).code(codeMessage.getCode()).message(codeMessage.getMessage()).build();
  }
 
  public static <D, C> ServiceResultBuilder<D, C> error() {
    ServiceResultBuilder<D, C> builder = builder();
    return builder.isSuccess(false);
  }
 
 
  static <D, C> ServiceResultBuilder<D, C> builder() {
    return new ServiceResultBuilder<>();
  }
 
  public static class DefaultMessage<C> implements CodeMessage<C>, java.io.Serializable {
 
    private C code;
    private String message;
 
    public DefaultMessage(C code, String message) {
      this.code = code;
      this.message = message;
    }
 
    @Override
    public C getCode() {
      return code;
    }
 
    @Override
    public String getMessage() {
      return message;
    }
 
    @Override
    public String toString() {
      return "DefaultMessage{" +
          "code=" + code +
          ", message='" + message + '\'' +
          '}';
    }
  }
 
  public static class ServiceResultBuilder<T, C> {
 
    private T data;
    private C code;
    private String message;
    private boolean isSuccess;
 
    ServiceResultBuilder() { //package private
    }
 
    public ServiceResultBuilder<T, C> data(T data) {
      this.data = data;
      return this;
    }
 
    ServiceResultBuilder<T, C> isSuccess(boolean isSuccess) {
      this.isSuccess = isSuccess;
      return this;
    }
 
    public ServiceResultBuilder<T, C> code(C code) {
      this.code = code;
      return this;
    }
 
    public ServiceResultBuilder<T, C> message(String message) {
      this.message = message;
      return this;
    }
 
    public ServiceResult<T, C> build() {
      Objects.requireNonNull(code, "code");
      Objects.requireNonNull(code, "message");
      return new ServiceResult<>(data, isSuccess, new DefaultMessage<>(code, message));
    }
  }
 
  @Override
  public String toString() {
    return "ServiceResult{" +
        "data=" + data +
        ", message=" + message +
        ", isSuccess=" + isSuccess +
        '}';
  }
}

3.使用

定义枚举错误码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pubilc enum CodeMessageEnum implements CodeMessage<Integer> {
  some_error(1001, "错误信息。");
  Integer code;
  String message;
 
  CodeMessageEnum(Integer code, String message) {
    this.code = code;
    this.message = message;
  }
 
  @Override
  public Integer getCode() {
    return code;
  }
 
  @Override
  public String getMessage() {
    return message;
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//返回正确结果带返回值。
 
ServiceResult<List<String>, String> s = ServiceResult.success(Arrays.asList("1", "2", "3"));
s.isSuccess(); //true
ServiceResult<?, Integer> e1 = ServiceResult.error(CodeMessageEnum.some_error);
e1.isSuccess(); //false
ServiceResult<?, Long> e2 = ServiceResult.error(new ServiceResult.DefaultMessage<>(1000L, "error"));
e2.isSuccess(); //false
//ServiceResult{data=[1, 2, 3], message=DefaultMessage{code=00000000, message='success'}, isSuccess=true}
System.out.println(s);
//ServiceResult{data=null, message=DefaultMessage{code=1001, message='错误信息。'}, isSuccess=false}
System.out.println(e1);
//ServiceResult{data=null, message=DefaultMessage{code=1000, message='error'}, isSuccess=false}
System.out.println(e2);

补充知识: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
public class ResponseMessage {
  private final static String STATUS_OK = "0";
  private final static String STATUS_ERROR = "1";
 
  private String status;   // 状态
  private Object data;  // 返回值
  private Exception e;  // 异常类捕获
  private String msg;  // 自定义信息
 
  public String getStatus() {
    return status;
  }
  public void setStatus(String status) {
    this.status = status;
  }
  public Object getData() {
    return data;
  }
  public void setData(Object data) {
    this.data = data;
  }
  public Exception getE() {
    return e;
  }
  public void setE(Exception e) {
    this.e = e;
  }
  public String getMsg() {
    return msg;
  }
  public void setMsg(String msg) {
    this.msg = msg;
  }
  public ResponseMessage() {
    super();
  }
  public ResponseMessage(String status, Object data, Exception e, String msg) {
    super();
    this.status = status;
    this.data = data;
    this.e = e;
    this.msg = msg;
  }
 
  /**
   * 成功的结果
   * @param data 返回结果
   * @param msg 返回信息
   */
  public static ResponseMessage newOkInstance(Object data, String msg) {
    return new ResponseMessage(ResponseMessage.STATUS_OK, data, null, msg);
  }
 
  /**
   * 成功的结果
   * @param data  返回结果
   */
  public ResponseMessage newOkInstance(Object data) {
    return new ResponseMessage(ResponseMessage.STATUS_OK, data, null, null);
  }
 
  /**
   * 失败的结果
   * @param msg 返回信息
   */
  public static ResponseMessage newErrorInstance(String msg) {
    return new ResponseMessage(ResponseMessage.STATUS_ERROR, null, null, msg);
  }
 
  /**
   * 失败的结果
   * @param e    异常对象
   * @param msg 返回信息
   */
  public static ResponseMessage newErrorInstance(Exception e, String msg) {
    return new ResponseMessage(ResponseMessage.STATUS_ERROR, null, e, msg);
  }
}

以上这篇Java封装统一的Result Model案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/m0_38043362/article/details/79696296

延伸 · 阅读

精彩推荐