当前位置:首页> 正文

如何将Object类转换为实体类

如何将Object类转换为实体类

目录

将Object类转换为实体类

问题描述

问题原因

解决办法

实体类之间的相互转换

将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;     }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持易知道(ezd.cc)。

展开全文阅读

相关内容