import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
/**
* Created by
* base类
*
*/
public class Base<T> {
private Integer code;//返回码
private String info;//返回信息
private T data;//返回值
public Base(Integer code, String info, T data) {
this.code = code;
this.info = info;
this.data = data;
}
public Base() {
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
/**
* 带信息的错误返回
* @param msg
* @return
*/
public static Base error(String msg) {
Base Base = new Base();
Base.setCode(500);
Base.setInfo(msg);
return Base;
}
/**
* 带编码和信息的错误返回
* @param code
* @param msg
* @return
*/
public static Base error(Integer code, String msg) {
Base Base = new Base();
Base.setCode(code);
Base.setInfo(msg);
return Base;
}
/**
* 异常返回,带具体错误编码和信息
*
* @param resultEnum
* @return
*/
public static Base error(ResultEnum resultEnum) {
Base Base = Base(resultEnum);
return Base;
}
/**
* 异常返回,带具体错误编码、信息和数据
*
* @param resultEnum
* @param data
* @return
*/
public static Base error(ResultEnum resultEnum, Object... data) {
Base Base = Base(resultEnum, data);
return Base;
}
/**
* 异常返回,带具体错误信息和返回数据
* @param msg
* @param data
* @return
*/
public static Base error(String msg, Object... data) {
Base Base = Base(ResultEnum.ERROR, data);
Base.setInfo(msg);
return Base;
}
/**
* 默认的成功返回
* @return
*/
public static Base success() {
Base Base = new Base();
Base.setCode(200);
Base.setInfo("操作成功");
return Base;
}
public static Base successM(Integer code,String msg,Object data) {
Base Base = new Base();
Base.setCode(code);
Base.setInfo(msg);
Base.setData(data);
return Base;
}
public static Base errorM(Integer code,String msg,Object data) {
Base Base = new Base();
Base.setCode(code);
Base.setInfo(msg);
Base.setData(data);
return Base;
}
/**
* 带信息的成功返回,用在增删改
* @param msg
* @return
*/
public static Base success(String msg) {
Base Base = success();
Base.setInfo(msg);
return Base;
}
/**
* 带数据的成功返回,用在查
* @param msg
* @param data
* @return
*/
public static Base success(String msg, Object... data) {
Base Base = successD(data);
Base.setInfo(msg);
return Base;
}
/**
* 带数据的成功返回,不自定义msg,用在查
* @param data
* @return
*/
public static Base successD(Object... data) {
Base Base = Base(ResultEnum.SUCCESS, data);
return Base;
}
/**
* 带信息返回
* @param resultEnum
* @return
*/
private static Base Base(ResultEnum resultEnum) {
Base Base = new Base();
Base.setCode(resultEnum.getCode());
Base.setInfo(resultEnum.getMessage());
return Base;
}
/**
* 帶信息、数据返回
*
* @param resultEnum
* @param data
* @return Base
*/
private static Base Base(ResultEnum resultEnum, Object... data) {
Base Base = new Base();
Base.setCode(resultEnum.getCode());
Base.setInfo(resultEnum.getMessage());
Integer length = data.length;
if (length == 1) {
Base.setData(data[0]);
} else {
JSONObject rs = new JSONObject();
for (int i = 0; i < length; i = i + 2) {
rs.put(data[i].toString(), data[i + 1]);
}
Base.setData(rs);
}
return Base;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
public static void main(String[] args) {
Base a = success("b", "a");
}
}
数据字典: 枚举
public enum ResultEnum {
SUCCESS(200, "成功"),
SUCCESS_SAVE(200, "保存成功"),
ERROR(500, "操作失败");
private Integer code;
private String message;
ResultEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}