当先锋百科网

首页 1 2 3 4 5 6 7
  • 存在的问题:协议未统一,缺少业务状态码。
  • JsonData工具类开发
    方法:在utils工具包里编写JsonData,定义状态码。
    项目结构
package com.gd.dwn.xddemoproject.utils;

public class JsonData {

    private int code;
    private Object data;
    private String msg;

    public JsonData(){}
    public JsonData(int code, Object data) {
        this.code = code;
        this.data = data;
    }

    public JsonData(int code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public static JsonData buildSuccess(Object data){
        return new JsonData(0,data);
    }
    public static JsonData buildError(String msg){
        return new JsonData(-1,"",msg);
    }
    public static JsonData buildError(String msg,int code){
        return new JsonData(0,"",msg);
    }

}