高复用服务相应对象的设计思想以及抽象封装
2017-09-09 23:43
447 查看
问题
在web开发中,后台开发与前端交互主要是通过json的方式,后台通过统一的返回样式,可以使前后端更好的交互,在一次项目中,我每次返回一个复杂对象的时候,都是用一个匿名对象序列化成json格式的数据返回前端,由于这个项目前后台都是我一个人完成,所以我能比较清楚返回的东西,但是如果是前后端分开进行开发或者前期是一个人开发,后期进行前后端离就会变得异常困难。 因此,规范返回的格式显得很重要,于是乎我便封装了一个简单的高复可用的对象用于响应数据。具体思路如下:
设计思路:
使用泛型让返回数据多样化,且可以让返回对象统一声明状态字段,表示服务响应状态
声明描述字段,描述当前状态的原因
声明泛型数据字段,用于返回多种类型的数据,实现高复用
主要代码如下:
import org.codehaus.jackson.annotate.JsonIgnore; import org.codehaus.jackson.map.annotate.JsonSerialize; import java.io.Serializable; /** * Created by *** on 2017/8/16. */ @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) //项目使用的是Jackson包,添加该注解,保证json序列化的时候,不返回key-null这种类型的数据 public class ServerResponse<T> implements Serializable { private int status; private String msg; private T data; //私有化构造方法,通过响应状态来创建 private ServerResponse(int status){ this.status = status; } private ServerResponse(int status,T data){ this.status = status; this.data = data; } private ServerResponse(int status,String msg,T data){ this.status = status; this.msg = msg; this.data = data; } private ServerResponse(int status,String msg){ this.status = status; this.msg = msg; } @JsonIgnore //json序列化返回前端的时候,忽略该方法,不需要返回 public boolean isSuccess() { return this.status == ResponseCode.SUCCESS.getCode(); } //把字段封装成属性,开放get方法 public int getStatus(){ return status; } public String getMsg(){ return msg; } public T getData(){ return data; } //声明创建对象的静态方法,通过下面这些方法来调用私有构造方法,并且set状态 //状态被声明在枚举ResponseCode中 public static <T> ServerResponse<T> createBySuccess(){ return new ServerResponse<T>(ResponseCode.SUCCESS.getCode()); } public static <T> ServerResponse<T> createBySuccessMessage(String msg){ return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg); } public static <T> ServerResponse<T> createBySuccess(T data){ return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data); } public static <T> ServerResponse<T> createBySuccess(String msg,T data){ return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data); } public static <T> ServerResponse<T> createByError(){ return new ServerResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc()); } public static <T> ServerResponse<T> createByErrorMessage(String errorMessage){ return new ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMessage); } public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode,String errorMessage){ return new ServerResponse<T>(errorCode,errorMessage); } }
下面是ResponseCode的主要代码
public enum ResponseCode { SUCCESS(0,"SUCCESS"), ERROR(1,"ERROR"), NEED_LOGIN(10,"NEED_LOGIN"), ILLEGAL_ARGUMENT(2,"ILLEGAL_ARGUMENT"); private final int code; private final String desc; ResponseCode(int code,String desc){ this.code = code; this.desc = desc; } public int getCode(){ return this.code; } public String getDesc(){ return t 4000 his.desc; } }
总结:
使用ServeResponse这个响应类去进行json序列化,然后响应前端,或者用于后台的各层之间进行交互,提高了代码的可读性,并且增加代码的重用。PS:博文中如有什么不对的地方恳请大家指出,谢谢~
相关文章推荐
- 以MyBatis+SpringMVC3.0实现的,借鉴了Hibernate设计思想,采用封装、抽象、继承的设计思想,做到了数据与相应的操作的高内聚低耦合的实现
- java编程思想复用,多态,以及设计模式-复用篇
- java编程思想复用,多态,以及设计模式-多态
- 封装,面向对象设计编程思想
- JAVA基础篇 ,类 对象,,封装 , 继承 的抽象思想
- 面向对象的分析设计程序思想通过封装、继承、多态将程序耦合度降低,使程序更加灵活、容易修改和易于复用。
- 设计模式学习(一):java 面向对象特征:抽象、封装、多态、继承
- 可复用面向对象软件基础——设计模式(六)之原型模式
- 数据访问层组件设计以及选型意识流 第二次封装以及各种牢骚
- Android应用程序开发以及背后的设计思想深度剖析(5)
- java面向对象思想之接口、抽象
- LSM树由来、设计思想以及应用到HBase的索引
- Kafka环境搭建以及服务封装
- C++中的property库的设计与实现过程(三)——将对象封装到property里面
- java 之 面向对象思想设计原则及常见设计模式
- 我对什么是真正的对象,以及软件中的对象在分析阶段、设计阶段、实现阶段的一些看法
- 从一个对象实例看数据抽象与封装
- Silverlight游戏设计(Game Design):(五)面向对象的思想塑造游戏对象
- 面向对象介绍以及封装
- 【设计分享】一个面向对象思想的perl编程实例