From f0110d44e6de17816c4b10345353d1e955a96caa Mon Sep 17 00:00:00 2001 From: baishou <2710684311@qq.com> Date: Thu, 23 May 2019 17:30:17 +0800 Subject: [PATCH] 接口统一管理类和code状态类封装完成 --- springboot/src/main/java/com/sincre/springboot/ApiModel/Page.java | 33 +++++++++++++++++++++++++++++++++ springboot/src/main/java/com/sincre/springboot/ApiModel/YinShiResResult.java | 48 ++++++++++++++++-------------------------------- springboot/src/main/java/com/sincre/springboot/common/ResponseCode.java | 29 +++++++++++++++++++++++++++++ springboot/src/main/java/com/sincre/springboot/common/ServerResponse.java | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java | 9 +++++++-- 5 files changed, 206 insertions(+), 34 deletions(-) create mode 100644 springboot/src/main/java/com/sincre/springboot/ApiModel/Page.java create mode 100644 springboot/src/main/java/com/sincre/springboot/common/ResponseCode.java create mode 100644 springboot/src/main/java/com/sincre/springboot/common/ServerResponse.java diff --git a/springboot/src/main/java/com/sincre/springboot/ApiModel/Page.java b/springboot/src/main/java/com/sincre/springboot/ApiModel/Page.java new file mode 100644 index 0000000..3c476cc --- /dev/null +++ b/springboot/src/main/java/com/sincre/springboot/ApiModel/Page.java @@ -0,0 +1,33 @@ +package com.zjx.springboot02.apimodel; + +public class Page { + public Integer getTotal() { + return total; + } + + public void setTotal(Integer total) { + this.total = total; + } + + public Integer getPage() { + return page; + } + + public void setPage(Integer page) { + this.page = page; + } + + public Integer getSize() { + return size; + } + + public void setSize(Integer size) { + this.size = size; + } + + private Integer total; + private Integer page; + private Integer size; + + +} diff --git a/springboot/src/main/java/com/sincre/springboot/ApiModel/YinShiResResult.java b/springboot/src/main/java/com/sincre/springboot/ApiModel/YinShiResResult.java index 482565a..40c451e 100644 --- a/springboot/src/main/java/com/sincre/springboot/ApiModel/YinShiResResult.java +++ b/springboot/src/main/java/com/sincre/springboot/ApiModel/YinShiResResult.java @@ -1,17 +1,19 @@ package com.sincre.springboot.ApiModel; -import com.fasterxml.jackson.annotation.JsonInclude; +/** + * 萤石接口返回的Model + * @param + */ +public class YinShiResResult { -@JsonInclude(JsonInclude.Include.NON_EMPTY) -public class YinShiResResult { + private Page page ; + private T data; - private YinShiToken data; - - public YinShiToken getData() { + public T getData() { return data; } - public void setData(YinShiToken data) { + public void setData(T data) { this.data = data; } @@ -31,33 +33,15 @@ public class YinShiResResult { this.msg = msg; } - private String code; - private String msg; - - public class YinShiToken{ - - private String accessToken; - /** - * 精确到毫秒的时间戳 - */ - private Long expireTime; - - public String getAccessToken() { - return accessToken; - } - - public void setAccessToken(String accessToken) { - this.accessToken = accessToken; - } - - public Long getExpireTime() { - return expireTime; - } + public Page getPage() { + return page; + } - public void setExpireTime(Long expireTime) { - this.expireTime = expireTime; - } + public void setPage(Page page) { + this.page = page; } + private String code; + private String msg; } diff --git a/springboot/src/main/java/com/sincre/springboot/common/ResponseCode.java b/springboot/src/main/java/com/sincre/springboot/common/ResponseCode.java new file mode 100644 index 0000000..6c19f94 --- /dev/null +++ b/springboot/src/main/java/com/sincre/springboot/common/ResponseCode.java @@ -0,0 +1,29 @@ +package com.zjx.springboot02.Common; + +/** + * Created by Ziv + */ +public enum ResponseCode { + + SUCCESS(200, "SUCCESS"), + ERROR(500, "ServerInnerERROR"); + + + private final int code; + private final String desc; + + + ResponseCode(int code, String desc) { + this.code = code; + this.desc = desc; + } + + public int getCode() { + return code; + } + + public String getDesc() { + return desc; + } + +} diff --git a/springboot/src/main/java/com/sincre/springboot/common/ServerResponse.java b/springboot/src/main/java/com/sincre/springboot/common/ServerResponse.java new file mode 100644 index 0000000..14c0723 --- /dev/null +++ b/springboot/src/main/java/com/sincre/springboot/common/ServerResponse.java @@ -0,0 +1,121 @@ +package com.zjx.springboot02.Common; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; + + +import java.io.Serializable; + +/** + * Created by Ziv + */ +@JsonInclude(JsonInclude.Include.NON_EMPTY) + +public class ServerResponse implements Serializable { + + /** + * 分页时用到的总数量 + */ + private int total; + /** + * 操作状态码 + */ + private int code; + /** + * 状态信息 + */ + private String msg; + /** + * 返回的数据包 + */ + private T data; + + private ServerResponse(int code) { + this.code = code; + } + + private ServerResponse(int code, T data) { + this.code = code; + this.data = data; + } + + private ServerResponse(int code, String msg, T data) { + this.code = code; + this.msg = msg; + this.data = data; + } + + private ServerResponse(int total,int code, String msg, T data) { + this.total = total; + this.code = code; + this.msg = msg; + this.data = data; + } + private ServerResponse(int code, String msg) { + this.code = code; + this.msg = msg; + } + + @JsonIgnore + //使之不在json序列化结果当中 + public boolean isSuccess() { + return this.code == ResponseCode.SUCCESS.getCode(); + } + + public int getTotal() { + return total; + } + public int getCode() { + return code; + } + + public T getData() { + return data; + } + + public String getMsg() { + return msg; + } + + + public static ServerResponse createBySuccess() { + return new ServerResponse(ResponseCode.SUCCESS.getCode()); + } + + /* + 该方法对应的是private ServerResponse(int code,String msg)的构造方法 + */ + public static ServerResponse createBySuccessMessage(String msg) { + return new ServerResponse(ResponseCode.SUCCESS.getCode(), msg); + } + + /* + 该方法对应的是private ServerResponse(int code,T data)构造方法 + */ + public static ServerResponse createBySuccess(T data) { + return new ServerResponse(ResponseCode.SUCCESS.getCode(), data); + } + + public static ServerResponse createBySuccess(String msg, T data) { + return new ServerResponse(ResponseCode.SUCCESS.getCode(), msg, data); + } + + public static ServerResponse createBySuccess(Integer total,String msg, T data) { + return new ServerResponse(total,ResponseCode.SUCCESS.getCode(), msg, data); + } + + + public static ServerResponse createByError() { + return new ServerResponse(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc()); + } + + + public static ServerResponse createByErrorMessage(String errorMessage) { + return new ServerResponse(ResponseCode.ERROR.getCode(), errorMessage); + } + + public static ServerResponse createByErrorCodeMessage(int errorCode, String errorMessage) { + return new ServerResponse(errorCode, errorMessage); + } + +} diff --git a/springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java b/springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java index d8827bb..ea8772b 100644 --- a/springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java +++ b/springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java @@ -2,7 +2,10 @@ package com.sincre.springboot.controller; import com.alibaba.fastjson.JSON; +import com.sincre.springboot.ApiModel.YinShiResResult; import com.sincre.springboot.common.MD5; +import com.sincre.springboot.common.ResponseCode; +import com.sincre.springboot.common.ServerResponse; import com.sincre.springboot.utils.ApiHelper; import com.sincre.springboot.utils.CacheHelper; import com.sincre.springboot.utils.ResultUtils; @@ -85,7 +88,7 @@ public class YinShiController { @ApiOperation(value = "获取子账号信息列表") @GetMapping("getChildAccountList") - public String getChildAccountList(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize) { + public ServerResponse getChildAccountList(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize) { String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/list"; Map map = new HashMap<>(); @@ -96,7 +99,9 @@ public class YinShiController { map.put("pageSize", pageSize); String result = ApiHelper.doPost(url, new HashMap(), map); - return result; + YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class); + System.out.println(yinShiResResult.getPage().getTotal()); + return ServerResponse.createBySuccess(yinShiResResult.getPage().getTotal(), ResponseCode.SUCCESS.getDesc(),yinShiResResult.getData()); } @ApiOperation(value = "修改当前子账户密码") -- libgit2 0.21.0