使用 extends HashMap<String, Object>的方式
public class AjaxResult extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
public static final String CODE_TAG = "code";
public static final String MSG_TAG = "msg";
public static final String DATA_TAG = "data";
public AjaxResult(int code, String msg) {
put(CODE_TAG, code);
put(MSG_TAG, msg);
}
}
✅ 序列化/反序列化方便 - JSON库可以直接处理
✅ Map操作简便 - 可以直接使用所有Map方法
✅ 扩展性好 - 可以随意添加字段
✅ 兼容性好 - 可以被期望接收Map的API直接使用
缺点:
❌ 类型不安全 - 可以put任意值
❌ 字段定义不明确 - 没有编译时检查
❌ 难以重构 - 字符串键容易写错
使用类定义变量的
方式
public class AjaxResult {
private int code;
private String msg;
private Object data;
private Map<String, Object> extras; // 额外字段
// 构造方法
public AjaxResult(int code, String msg) {
this.code = code;
this.msg = msg;
}
// Getter/Setter
public int getCode() { return code; }
public void setCode(int code) { this.code = code; }
// ... 其他getter/setter
}
优点:
✅ 类型安全 - 编译器检查类型
✅ IDE支持好 - 自动补全、重构
✅ API明确 - 字段和方法明确
✅ 可封装 - 可以控制访问权限
缺点:
❌ 需要手动序列化 - 需要额外的序列化配置
❌ 扩展性差 - 不能动态添加字段
❌ 代码量大 - 需要getter/setter
混合方式(推荐)
/**
* 使用组合而非继承,结合两者的优点
*/
public class AjaxResult {
// 核心字段:类型安全
private int code;
private String msg;
private Object data;
// 额外字段:保持扩展性
private Map<String, Object> extras = new HashMap<>();
public AjaxResult(int code, String msg) {
this.code = code;
this.msg = msg;
}
public AjaxResult(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
// 核心字段的getter/setter
public int getCode() { return code; }
public void setCode(int code) { this.code = code; }
public String getMsg() { return msg; }
public void setMsg(String msg) { this.msg = msg; }
public Object getData() { return data; }
public void setData(Object data) { this.data = data; }
// 扩展字段操作方法
public void putExtra(String key, Object value) {
extras.put(key, value);
}
public Object getExtra(String key) {
return extras.get(key);
}
public Map<String, Object> getExtras() {
return new HashMap<>(extras);
}
// 转换为Map(用于序列化)
public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("code", code);
map.put("msg", msg);
if (data != null) {
map.put("data", data);
}
map.putAll(extras);
return map;
}
// 从Map创建(用于反序列化)
public static AjaxResult fromMap(Map<String, Object> map) {
AjaxResult result = new AjaxResult(
(int) map.getOrDefault("code", 200),
(String) map.getOrDefault("msg", "")
);
result.setData(map.get("data"));
// 移除核心字段,剩下的作为扩展字段
Map<String, Object> remaining = new HashMap<>(map);
remaining.remove("code");
remaining.remove("msg");
remaining.remove("data");
result.extras.putAll(remaining);
return result;
}
}
使用Lombok简化
import lombok.Data;
import lombok.NoArgsConstructor;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.HashMap;
import java.util.Map;
@Data
@NoArgsConstructor
public class AjaxResult {
private int code = 200;
private String msg = "success";
private Object data;
// 动态字段
@JsonIgnore
private Map<String, Object> dynamicFields = new HashMap<>();
public AjaxResult(int code, String msg) {
this.code = code;
this.msg = msg;
}
public AjaxResult(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
// JSON序列化时包含动态字段
@JsonAnyGetter
public Map<String, Object> getDynamicFields() {
return dynamicFields;
}
// JSON反序列化时接收未知字段
@JsonAnySetter
public void setDynamicField(String key, Object value) {
dynamicFields.put(key, value);
}
// 链式调用
public AjaxResult code(int code) {
this.code = code;
return this;
}
public AjaxResult msg(String msg) {
this.msg = msg;
return this;
}
public AjaxResult data(Object data) {
this.data = data;
return this;
}
public AjaxResult put(String key, Object value) {
dynamicFields.put(key, value);
return this;
}
}
若依框架的实践
extends HashMap<String, Object>
package com.ruoyi.common.core.domain;
import java.util.HashMap;
import java.util.Objects;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.StringUtils;
/**
* 操作消息提醒
*
* @author ruoyi
*/
public class AjaxResult extends HashMap<String, Object>
{
private static final long serialVersionUID = 1L;
/** 状态码 */
public static final String CODE_TAG = "code";
/** 返回内容 */
public static final String MSG_TAG = "msg";
/** 数据对象 */
public static final String DATA_TAG = "data";
/**
* 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
*/
public AjaxResult()
{
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
*/
public AjaxResult(int code, String msg)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
* @param data 数据对象
*/
public AjaxResult(int code, String msg, Object data)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
if (StringUtils.isNotNull(data))
{
super.put(DATA_TAG, data);
}
}
/**
* 返回成功消息
*
* @return 成功消息
*/
public static AjaxResult success()
{
return AjaxResult.success("操作成功");
}
/**
* 返回成功数据
*
* @return 成功消息
*/
public static AjaxResult success(Object data)
{
return AjaxResult.success("操作成功", data);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @return 成功消息
*/
public static AjaxResult success(String msg)
{
return AjaxResult.success(msg, null);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 成功消息
*/
public static AjaxResult success(String msg, Object data)
{
return new AjaxResult(HttpStatus.SUCCESS, msg, data);
}
/**
* 返回警告消息
*
* @param msg 返回内容
* @return 警告消息
*/
public static AjaxResult warn(String msg)
{
return AjaxResult.warn(msg, null);
}
/**
* 返回警告消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 警告消息
*/
public static AjaxResult warn(String msg, Object data)
{
return new AjaxResult(HttpStatus.WARN, msg, data);
}
/**
* 返回错误消息
*
* @return 错误消息
*/
public static AjaxResult error()
{
return AjaxResult.error("操作失败");
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @return 错误消息
*/
public static AjaxResult error(String msg)
{
return AjaxResult.error(msg, null);
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 错误消息
*/
public static AjaxResult error(String msg, Object data)
{
return new AjaxResult(HttpStatus.ERROR, msg, data);
}
/**
* 返回错误消息
*
* @param code 状态码
* @param msg 返回内容
* @return 错误消息
*/
public static AjaxResult error(int code, String msg)
{
return new AjaxResult(code, msg, null);
}
/**
* 是否为成功消息
*
* @return 结果
*/
public boolean isSuccess()
{
return Objects.equals(HttpStatus.SUCCESS, this.get(CODE_TAG));
}
/**
* 是否为警告消息
*
* @return 结果
*/
public boolean isWarn()
{
return Objects.equals(HttpStatus.WARN, this.get(CODE_TAG));
}
/**
* 是否为错误消息
*
* @return 结果
*/
public boolean isError()
{
return Objects.equals(HttpStatus.ERROR, this.get(CODE_TAG));
}
/**
* 方便链式调用
*
* @param key 键
* @param value 值
* @return 数据对象
*/
@Override
public AjaxResult put(String key, Object value)
{
super.put(key, value);
return this;
}
}
private Map<String, Object> params;
package com.ruoyi.common.core.domain;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
/**
* Entity基类
*
* @author ruoyi
*/
public class BaseEntity implements Serializable
{
private static final long serialVersionUID = 1L;
/** 搜索值 */
@JsonIgnore
private String searchValue;
/** 创建者 */
private String createBy;
/** 创建时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/** 更新者 */
private String updateBy;
/** 更新时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/** 备注 */
private String remark;
/** 请求参数 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Map<String, Object> params;
public String getSearchValue()
{
return searchValue;
}
public void setSearchValue(String searchValue)
{
this.searchValue = searchValue;
}
public String getCreateBy()
{
return createBy;
}
public void setCreateBy(String createBy)
{
this.createBy = createBy;
}
public Date getCreateTime()
{
return createTime;
}
public void setCreateTime(Date createTime)
{
this.createTime = createTime;
}
public String getUpdateBy()
{
return updateBy;
}
public void setUpdateBy(String updateBy)
{
this.updateBy = updateBy;
}
public Date getUpdateTime()
{
return updateTime;
}
public void setUpdateTime(Date updateTime)
{
this.updateTime = updateTime;
}
public String getRemark()
{
return remark;
}
public void setRemark(String remark)
{
this.remark = remark;
}
public Map<String, Object> getParams()
{
if (params == null)
{
params = new HashMap<>();
}
return params;
}
public void setParams(Map<String, Object> params)
{
this.params = params;
}
}

被折叠的 条评论
为什么被折叠?



