如何將Object類轉(zhuǎn)換為實體類
將Object類轉(zhuǎn)換為實體類
問題描述
在用SpringBoot寫controller的時候,需要接受一個map的Object,之后要把Object轉(zhuǎn)為特定的類,代碼如下:
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轉(zhuǎn)為特定的實體類
解決辦法
需要通過json來作為中間介質(zhì):
? ?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); ? ? }
實體類之間的相互轉(zhuǎn)換
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(); ? ? } ? ? /** ? ? ?* 可實現(xiàn)由 BeanA List 轉(zhuǎn)換為 BeanB List<br> ? ? ?* tip1: 轉(zhuǎn)換的規(guī)則是 實體內(nèi)屬性一致的進(jìn)行轉(zhuǎn)換<br> ? ? ?* tip2: 轉(zhuǎn)換會忽略 Null 和錯誤 ? ? ?* ? ? ?* @param listA ? ? ? ? ? ?A 實體 ? ? ?* @param bClass ? ? ? ? ? B 類 ? ? ?* @param ignoreProperties 要忽略轉(zhuǎn)換的字段 數(shù)組類型<br> ? ? ?* ? ? ? ? ? ? ? ? ? ? ? ? 由該屬性可解決同一個Vo 在不同需求中要返回的實體不一致問題 列入UserListVO 在后臺和前臺使用的列表是同一個,但是返回的字段不一致 ? ? ?* @param <A> 泛型A ? ? ?* @param <B> 泛型 ? ? ?* @return 轉(zhuǎn)換后的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; ? ? }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java關(guān)于后端怎么去接收Date、LocalDateTime類型的參數(shù)詳解
這篇文章主要介紹了java關(guān)于后端怎么去接收Date、LocalDateTime類型的參數(shù),文中有詳細(xì)的代碼流程,對我們學(xué)習(xí)或工作有一定的參考價值,需要的朋友可以參考下2023-06-06Mybatis-plus動態(tài)條件查詢QueryWrapper的使用案例
mybatis-plus框架功能很強大,把很多功能都集成了,下面這篇文章主要給大家介紹了關(guān)于Mybatis-plus動態(tài)條件查詢QueryWrapper的使用教程,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07淺談synchronized方法對非synchronized方法的影響
下面小編就為大家?guī)硪黄獪\談synchronized方法對非synchronized方法的影響。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10