在AjaxResult 里获取data字段并将其转换为指定类型的对象
This commit is contained in:
parent
dc7a51eba6
commit
537cfe5c9b
|
|
@ -153,6 +153,11 @@
|
||||||
<artifactId>itext-asian</artifactId>
|
<artifactId>itext-asian</artifactId>
|
||||||
<version>5.2.0</version>
|
<version>5.2.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.8.9</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mockito</groupId>
|
<groupId>org.mockito</groupId>
|
||||||
<artifactId>mockito-core</artifactId>
|
<artifactId>mockito-core</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import java.util.HashMap;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import com.bonus.common.core.constant.HttpStatus;
|
import com.bonus.common.core.constant.HttpStatus;
|
||||||
import com.bonus.common.core.utils.StringUtils;
|
import com.bonus.common.core.utils.StringUtils;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 操作消息提醒
|
* 操作消息提醒
|
||||||
|
|
@ -213,4 +214,25 @@ public class AjaxResult extends HashMap<String, Object>
|
||||||
super.put(key, value);
|
super.put(key, value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取data字段并将其转换为指定类型的对象
|
||||||
|
*
|
||||||
|
* @param clazz 目标类
|
||||||
|
* @param <T> 目标类型
|
||||||
|
* @return 转换后的对象,或null如果data字段为空
|
||||||
|
*/
|
||||||
|
public <T> T getDataAs(Class<T> clazz) {
|
||||||
|
Object data = this.get(DATA_TAG); // 获取data字段
|
||||||
|
if (data == null) {
|
||||||
|
return null; // 如果data为空,返回null
|
||||||
|
}
|
||||||
|
// 创建Gson实例
|
||||||
|
Gson gson = new Gson();
|
||||||
|
|
||||||
|
// 将data从LinkedHashMap转换为指定类型的对象
|
||||||
|
return gson.fromJson(gson.toJson(data), clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue