0
点赞
收藏
分享

微信扫一扫

封装统一返回数据格式

import java.util.HashMap;
import java.util.Map;


/**
* 操作消息提醒
*
*/
public class AjaxResult extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;

/**
* 初始化一个新创建的 Message 对象
*/
public AjaxResult() {
}

/**
* 返回错误消息
*
* @return 错误消息
*/
public static AjaxResult error() {
return error(1, "操作失败");
}

/**
* 返回错误消息
*
* @param msg 内容
* @return 错误消息
*/
public static AjaxResult error(String msg) {
return error(500, msg);
}

/**
* 返回错误消息
*
* @param code 错误码
* @param msg 内容
* @return 错误消息
*/
public static AjaxResult error(int code, String msg) {
AjaxResult json = new AjaxResult();
json.put("code", code);
json.put("msg", msg);
return json;
}

/**
* 返回错误消息
*
* @param code 错误码
* @param msg 错误消息
* @param data 附加内容
* @return 错误消息
*/
public static AjaxResult error(int code, String msg, Object data) {
AjaxResult json = new AjaxResult();
json.put("code", code);
json.put("msg", msg);
json.put("data", data);
return json;
}

/**
* 返回成功消息
*
* @param msg 内容
* @return 成功消息
*/
public static AjaxResult success(String msg) {
AjaxResult json = new AjaxResult();
json.put("msg", msg);
json.put("code", 0);
return json;
}

/**
* 返回成功 带对象
*
* @param data 对象内容
* @return 成功消息
*/
public static AjaxResult success(Object data) {
AjaxResult json = new AjaxResult();
json.put("msg", "");
json.put("code", 0);
json.put("data", data);

return json;
}

/**
* 返回成功 带map
*
* @param map 对象内容
* @return 成功消息
*/
public static AjaxResult success(Map<String, Object> map) {
AjaxResult json = new AjaxResult();
json.put("msg", "");
json.put("code", 0);
json.putAll(map);

return json;
}

/**
* 返回成功 带对象
*
* @param data 对象内容
* @return 成功消息
*/
public static AjaxResult success(Object[] data) {
AjaxResult json = new AjaxResult();
json.put("msg", "");
json.put("code", 0);
json.put("data", data);

return json;
}

/**
* 返回成功消息
*
* @return 成功消息
*/
public static AjaxResult success() {
return AjaxResult.success("操作成功");
}

/**
* 返回成功消息
*
* @param key 键值
* @param value 内容
* @return 成功消息
*/
@Override
public AjaxResult put(String key, Object value) {
super.put(key, value);
return this;
}

}


举报

相关推荐

0 条评论