一、高复用服务响应对象

一、 高复用服务响应对象

  1 package com.mmall.common;
  2 
  3 import com.fasterxml.jackson.annotation.JsonIgnore;
  4 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
  5 
  6 import java.io.Serializable;
  7 
  8 /**
  9  * 高复用服务响应对象
 10  *
 11  * Created by ly on 2018/6/11.
 12  */
 13 @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
 14 public class ServerResponse<T> implements Serializable {
 15 
 16     /**
 17      * 状态码
 18      */
 19     private  int status;
 20 
 21     /**
 22      * 响应信息
 23      */
 24     private  String msg;
 25 
 26     /**
 27      * 响应数据
 28      */
 29     private T data;
 30 
 31     /**
 32      * 构造函数
 33      *
 34      * @param status
 35      */
 36     private ServerResponse(int status) {
 37         this.status = status;
 38     }
 39 
 40     private ServerResponse(int status, String msg) {
 41         this.status = status;
 42         this.msg = msg;
 43     }
 44 
 45     private ServerResponse(int status, T data) {
 46         this.status = status;
 47         this.data = data;
 48     }
 49 
 50     private ServerResponse(int status, String msg, T data) {
 51         this.status = status;
 52         this.msg = msg;
 53         this.data = data;
 54     }
 55 
 56     /**
 57      * 根据状态码返回是否成功
 58      * JsonIgnore使被注解内容不在json序列化结果当中
 59      *
 60      * @return
 61      */
 62     @JsonIgnore
 63     public boolean isSuccess() {
 64         return this.status == ResponseCode.SUCCESS.getCode();
 65     }
 66 
 67     /**
 68      * 暴露获取属性的方法
 69      *
 70      * @return
 71      */
 72     public  int getStatus() {
 73         return  this.status;
 74     }
 75 
 76     public  String getMsg() {
 77         return  this.msg;
 78     }
 79 
 80     public  T getData() {
 81         return  this.data;
 82     }
 83 
 84     /**
 85      * 通过该公开方法直接获取一个成功状态码的本对象
 86      *
 87      * @param <T>
 88      * @return
 89      */
 90     public static <T> ServerResponse<T> createBySuccess() {
 91         return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
 92     }
 93 
 94     public static <T> ServerResponse<T> createBySuccess(String msg) {
 95         return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), msg);
 96     }
 97 
 98     public static <T> ServerResponse<T> createBySuccess(T data) {
 99         return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), data);
100     }
101 
102     public static <T> ServerResponse<T> createBySuccess(String msg, T data) {
103         return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), msg, data);
104     }
105 
106     /**
107      * 获取错误对象
108      *
109      * @param <T>
110      * @return
111      */
112     public static <T> ServerResponse<T> createByError() {
113         return new ServerResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
114     }
115 
116     public static <T> ServerResponse<T> createByError(String msg) {
117         return new ServerResponse<T>(ResponseCode.ERROR.getCode(), msg);
118     }
119 
120     public static <T> ServerResponse<T> createByError(int errorCode, String errorMessage) {
121         return new ServerResponse<T>(errorCode, errorMessage);
122     }
123 
124 }
View Code

二、 响应枚举类

 1 package com.mmall.common;
 2 
 3 /**
 4  * 响应的枚举类
 5  *
 6  * Created by ly on 2018/6/11.
 7  */
 8 public enum  ResponseCode {
 9 
10     SUCCESS(0,"SUCCESS"),
11     ERROR(1,"ERROR"),
12     NEED_LOGIN(10,"NEED_LOGIN"),
13     ILLEGAL_ARGUMENT(2,"ILLEGAL_ARGUMENT");
14 
15     private final int code;
16     private final String desc;
17 
18     // 这里使用default的修饰,只允许类内部及本包调用
19     ResponseCode(int code, String desc) {
20         this.code = code;
21         this.desc = desc;
22     }
23 
24     /**
25      * 暴露获取属性的方法
26      *
27      * @return
28      */
29     public int getCode() {
30         return code;
31     }
32     public String getDesc() {
33         return desc;
34     }
35 }
View Code

猜你喜欢

转载自www.cnblogs.com/liyue-sqsf/p/9175627.html