如何将Object类转换为实体类
作者:王星星的魔灯 时间:2021-11-05 04:45:11
将Object类转换为实体类
问题描述
在用SpringBoot写controller的时候,需要接受一个map的Object,之后要把Object转为特定的类,代码如下:
public boolean postArticle(@RequestBody Map<String, Object> map) {
ArticleInfo articleInfo = (ArticleInfo) map.get("articleInfo");
ArticleContent articleContent = (ArticleContent) map.get("articleContent");
System.out.println(articleInfo + " " + articleContent);
return true;
}
之后爆出异常:
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class
cn.zi10ng.blog.domain.ArticleInfo (java.util.LinkedHashMap is in module java.base of loader
'bootstrap'; cn.zi10ng.blog.domain.ArticleInfo is in unnamed module of loader
org.springframework.boot.devtools.restart.classloader.RestartClassLoader @19b54dc3)
问题原因
map中取出的是Object,不能直接把Object转为特定的实体类
解决办法
需要通过json来作为中间介质:
public boolean postArticle(@RequestBody Map<String, Object> map) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String jsonInfo = objectMapper.writeValueAsString(map.get("articleInfo"));
String jsonContent = objectMapper.writeValueAsString(map.get("articleContent"));
ArticleInfo articleInfo = objectMapper.readValue(jsonInfo,ArticleInfo.class);
ArticleContent articleContent = objectMapper.readValue(jsonContent,ArticleContent.class);
System.out.println(articleContent + " " +articleInfo);
return articleService.insertArticle(articleInfo,articleContent);
}
实体类之间的相互转换
public static <A, B> B beanA2beanB(A beanA, Class<B> bClass, String... ignoreProperties) {
try {
B b = bClass.newInstance();
cn.hutool.core.bean.BeanUtil.copyProperties(
beanA,
b,
CopyOptions.create().setIgnoreProperties(ignoreProperties).ignoreError().ignoreNullValue()
);
return b;
} catch (Exception e) {
e.printStackTrace();
}
return (B) new Object();
}
/**
* 可实现由 BeanA List 转换为 BeanB List<br>
* tip1: 转换的规则是 实体内属性一致的进行转换<br>
* tip2: 转换会忽略 Null 和错误
*
* @param listA A 实体
* @param bClass B 类
* @param ignoreProperties 要忽略转换的字段 数组类型<br>
* 由该属性可解决同一个Vo 在不同需求中要返回的实体不一致问题 列入UserListVO 在后台和前台使用的列表是同一个,但是返回的字段不一致
* @param <A> 泛型A
* @param <B> 泛型
* @return 转换后的BList实体
*/
public static <A, B> List<B> listA2ListB(Collection<A> listA, Class<B> bClass, String... ignoreProperties) {
List<B> listB = new ArrayList<>();
if (ObjectUtils.isEmpty(listA)) {
return listB;
}
try {
for (A a : listA) {
listB.add(beanA2beanB(a, bClass, ignoreProperties));
}
} catch (Exception e) {
e.printStackTrace();
}
return listB;
}
来源:https://wxxlamp.blog.csdn.net/article/details/97901077
标签:Object,转换,实体类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android token过期刷新处理的方法示例
2023-11-23 14:11:40
线程阻塞唤醒工具 LockSupport使用详解
2023-11-29 17:16:10
C# 委托的三种调用示例(同步调用 异步调用 异步回调)
2023-06-09 10:29:56
Iconfont(矢量图标)+iconmoon(图标svg互转)配合javascript实现社交分享系统
2023-09-28 13:23:19
![](https://img.aspxhome.com/file/2023/0/64290_0s.png)
IDEA设置背景为自定义照片的操作方法
2022-12-28 09:13:08
![](https://img.aspxhome.com/file/2023/5/61645_0s.jpg)
Android实现Service重启的方法
2021-10-01 09:25:25
详解Java线程同步器CountDownLatch
2023-08-23 18:42:39
![](https://img.aspxhome.com/file/2023/2/58342_0s.png)
使用idea解决maven依赖冲突的问题
2021-10-16 12:32:51
![](https://img.aspxhome.com/file/2023/0/71160_0s.png)
SpringBoot集成Jpa对数据进行排序、分页、条件查询和过滤操作
2022-03-06 19:17:50
![](https://img.aspxhome.com/file/2023/7/70507_0s.png)
Android实现摄像头拍照功能
2022-04-25 13:05:55
Spring Boot配置AOP打印日志的全过程
2023-08-07 12:56:38
IntelliJ IDEA Run时报“无效的源发行版:16“错误问题及解决方法
2022-06-04 18:08:35
![](https://img.aspxhome.com/file/2023/5/62455_0s.png)
MyBatis的 config.xml标签
2021-07-18 02:01:34
详解Java编程中线程同步以及定时启动线程的方法
2021-08-31 10:59:20
SpringBoot集成Redisson实现分布式锁的方法示例
2021-10-30 16:59:38
![](https://img.aspxhome.com/file/2023/8/83488_0s.jpg)
C#格式化json字符串的方法分析
2023-12-23 00:20:36
c# Selenium爬取数据时防止webdriver封爬虫的方法
2023-06-24 07:50:51
![](https://img.aspxhome.com/file/2023/7/59117_0s.png)
opencv实现轮廓高斯滤波平滑
2023-07-23 02:05:56
![](https://img.aspxhome.com/file/2023/4/82534_0s.jpg)
idea无法切换分支报错问题及解决
2023-12-14 22:39:26
![](https://img.aspxhome.com/file/2023/2/64552_0s.png)
Java+swing实现抖音上的表白程序详解
2023-11-15 21:34:08
![](https://img.aspxhome.com/file/2023/6/76196_0s.png)