您的位置:首页 > 其它

高复用服务相应对象的设计思想以及抽象封装

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:博文中如有什么不对的地方恳请大家指出,谢谢~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  设计 web开发 对象
相关文章推荐