高复用的面向接口编程接口设计(以用户开发过程为例)
来源:互联网 发布:淘宝超级快车好用吗 编辑:程序博客网 时间:2024/06/07 22:17
在用户模块开发过程中经常要想前台传递数据信息,假设这个json格式是这样的
{
state:0,
msg:"success",
data:{一堆不同类型的数据}
}
每次弄不同的东西的话,很烦。为了实现这个功能就设计了一个高复用的接口类ServiceResponse,并将其json序列化:
package com.mmal.common;import org.codehaus.jackson.annotate.JsonIgnore;import org.codehaus.jackson.map.annotate.JsonSerialize;import org.omg.CORBA.Object;import java.io.Serializable;/** * Created by Administrator on 2017/9/4. */@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)//保证序列化json的时候,如果是null,key也会消失public class ServiceResponse <T> implements Serializable { private int status; private String msg; private T data; private ServiceResponse(int status){ this.status=status; } private ServiceResponse(int status,T data){ this.status =status; this.data=data; } private ServiceResponse(int status,String msg,T data){ this.status =status; this.msg =msg; this.data = data; } private ServiceResponse(int status,String msg){ this.status =status; this.msg=msg; } @JsonIgnore //使之不在序列化结果之中 public boolean isSucess(){ return this.status==ResponseCode.SUCCESS.getCode(); } public int getStatus(){ return status; } public String getMsg(){ return msg; } public T getData(){ return data; } public static <T> ServiceResponse<T> CreateBySuccess(){ return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode()); } public static <T> ServiceResponse<T> CreateBySuccessMessage(String msg){ return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(),msg); } public static <T> ServiceResponse<T> CreateBySuccess(T data){ return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(),data); } public static <T> ServiceResponse<T> CreateBySuccess(String msg,T data){ return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data); } public static <T> ServiceResponse<T> CreateByError(){ return new ServiceResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc()); } public static <T> ServiceResponse<T> CreateByErrorMessage(String errormsg){ return new ServiceResponse<T>(ResponseCode.ERROR.getCode(),errormsg); } //报一个变量的错误 public static <T> ServiceResponse<T> CreateByErrorCodeMessage(int errorCode,String errorMessage){ return new ServiceResponse<T>(errorCode,errorMessage); }}同时还需要一个枚举类ResponseCode:
package com.mmal.common;/** * Created by Administrator on 2017/9/4. */public enum ResponseCode { SUCCESS(0,"SUCCESS"), ERROR(1,"ERROR"), NEED_LOGIN(10,"NEED_LOGIN"), ILLEAGAL_ARGUMENT(2,"ILLEAGAL_ARGUMENT"); private final int code; private final String desc; private ResponseCode(int code,String desc){ this.code = code; this.desc=desc; } public int getCode(){ return code; } public String getDesc(){ return desc; }}
阅读全文
0 0
- 高复用的面向接口编程接口设计(以用户开发过程为例)
- 面向接口设计和编程——(面向对象、面向接口、面向过程、面向实现)
- 面向接口编程的设计模式
- 设计原则-----------面向接口编程
- 面向接口 的编程
- 面向接口的编程
- 面向接口的编程
- 面向接口的编程
- 蓝牙(BLE)应用框架接口设计和应用开发——以TI CC2541为例
- 多接口设计应以接口为个体进行设计
- 面向接口编程,接口的优点
- 面向接口的编程(WCF)
- 面向接口的编程(WCF)
- 勃大精深的设计模式--面向接口编程
- 面向接口编程及面向接口编程的好处
- 面向接口编程以及面向接口编程的好处
- 面向接口的编程思想
- 面向接口编程的好处
- 使用Files.lines遇到文件编码带bom的问题
- 2017 icpc 北京赛区 I.Minimum(线段树)
- 幼儿园门口摆个考勤机 家长接孩子得打卡
- 第四周项目1---建立单链表
- 2017.9.23 Count on a tree 思考记录
- 高复用的面向接口编程接口设计(以用户开发过程为例)
- 近期遇到的UBUNTU lamp配置问题及总结
- 深入理解Java虚拟机读书笔记--第七章 虚拟机类加载机制
- 欢迎使用CSDN-markdown编辑器
- The C Programming Language 练习题1-24
- android app性能优化
- 使用struts2 由pojo类跳转到jsp页面再跳转到jsp页面使用后退时,报空指针异常
- redis加锁的几种实现
- JSP基础(七)——JSP的9个内置对象