java 通过泛型 实现接口api的封装,避免频繁的继承以及重写。
json示例
{
"retcode":0,
"message":"操作成功",
"data":{
"total":2,
"list":[
{
"id":5003665,
"name":"锦江酒店",
"adType":2,
"mediaType":0,
"dailyBudget":-1,
"createDate":"2019-07-31 15:50:41",
"deleted":0,
"pauseState":1,
"status":2,
"statusDesc":"已暂停",
"activityId":0
},
{
"id":5003374,
"name":"锦江酒店-推荐列表",
"adType":2,
"mediaType":0,
"dailyBudget":200,
"createDate":"2019-07-30 18:53:33",
"deleted":0,
"pauseState":1,
"status":2,
"statusDesc":"已暂停",
"activityId":0
}
]
}
}
我们想将这个json封装为实体bean
1、首先最外层是retcode、message、data。data的类型是活的,这次返回的是计划、下次可以返回广告,所以把他定义为泛型
public class VivoV2Repsonse<T>{
private Integer retcode;
private String message;
private T data;
public Integer getRetcode() {
return retcode;
}
public void setRetcode(Integer retcode) {
this.retcode = retcode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
2、最外层封装完,看data的里面是total、list,然而list里面的内容还是活的,所以还是定义为泛型
public class Data<T> {
private Integer total;
private List<T> list;
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
3、定义list里面的内容
public class Plan {
private String id;
private String userId;
private String name;
private Integer adType;
private Integer dailyBudget;
private String createDate;
private Integer deleted;
private Integer pauseState;
private Integer status;
private String statusDesc;
private Integer activityId;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAdType() {
return adType;
}
public void setAdType(Integer adType) {
this.adType = adType;
}
public Integer getDailyBudget() {
return dailyBudget;
}
public void setDailyBudget(Integer dailyBudget) {
this.dailyBudget = dailyBudget;
}
public String getCreateDate() {
return createDate;
}
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
public Integer getDeleted() {
return deleted;
}
public void setDeleted(Integer deleted) {
this.deleted = deleted;
}
public Integer getPauseState() {
return pauseState;
}
public void setPauseState(Integer pauseState) {
this.pauseState = pauseState;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getStatusDesc() {
return statusDesc;
}
public void setStatusDesc(String statusDesc) {
this.statusDesc = statusDesc;
}
public Integer getActivityId() {
return activityId;
}
public void setActivityId(Integer activityId) {
this.activityId = activityId;
}
}
这样 就封装完了
测试------通过gson进行返回结果封装
比如我们下载获取的是广告数据
VivoV2Repsonse<Data<Ad>> response = new VivoV2Repsonse<Data<Ad>>();
我们只需要把plan改成ad就好了、很节省我们封装api的时间。。。