本案例基于 Springboot 2.5.7 单元测试场景下进行
<!-- SpringMVC默认使用Jacson , 只需要引用web启动器即可,无序单独引用Jackson --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Springboot单元测试 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!-- Lombok工具类 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!-- Hutool工具类 --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.3</version></dependency>
在后面的测试中会用到的实体类
@Data@NoArgsConstructor@AllArgsConstructorpublic class UserEntity {private Integer id;private String username;private String password;private Date birthday;private LocalDateTime lastLoginDate;private DeptEntity dept;}
@Data@NoArgsConstructor@AllArgsConstructorpublic class DeptEntity {private Integer id;private String name;}
@Data@NoArgsConstructor@AllArgsConstructorpublic class Result<T> {private int code;private String msg;private T data;public static <T> Result<T> success(T data) {return new Result<>(200, "请求成功", data);}}
IOC 容器中可以直接获取到 Jackson 的 ObjectMapper 实例
@SpringBootTestpublic class SpringTest {@Autowiredprivate ObjectMapper mapper;}
基础类型转换简单来说就是实体类转换,无论是实体类还是实体类嵌套方法都是一样的
实体类转换
@Testvoid test() throws JsonProcessingException {// 实体类DeptEntity dept = new DeptEntity(10001, "部门A");// 序列化String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(dept);// 反序列化System.out.println(mapper.readValue(json, DeptEntity.class));}
实体类嵌套转换
@Testvoid test() {// 实体类Date birthday = new Date();LocalDateTime lastLoginDate = LocalDateTime.now();DeptEntity dept = new DeptEntity(10001, "部门A");UserEntity user = new UserEntity(10001, "用户A", null, birthday, lastLoginDate, dept);// 序列化String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);// 反序列化System.out.println(mapper.readValue(json, UserEntity.class));}
集合类型转换集合转换简单了解这两种就够了,复杂一点的后面会提到Collection 集合转换
@Testvoid test() throws JsonProcessingException {// 构建List集合List<DeptEntity> source = CollUtil.newArrayList();for (int i = 1; i <= 5; i++) {source.add(new DeptEntity(10000 + i, "用户" + i));}// 序列化String json = mapper.writeValueAsString(source);// 构建Type对象CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, DeptEntity.class);// 反序列化List<DeptEntity> target = mapper.readValue(json, type);System.out.println(target);}
Map 集合转换
@Testvoid test() throws JsonProcessingException {// 构建List集合Map<String, String> source = MapUtil.newHashMap();source.put("aaa", "哈哈");source.put("bbb", "呵呵");// 序列化String json = mapper.writeValueAsString(source);// 构建Type对象MapLikeType type = mapper.getTypeFactory().constructMapLikeType(HashMap.class, String.class, String.class);// 反序列化Map<String, String> target = mapper.readValue(json, type);System.out.println(target);}
复杂类型转换这个部分的功能掌握了,类型转换就基本没啥问题了带有泛型的序列化
@Testvoid test() throws JsonProcessingException {// 实体类Result<DeptEntity> source = Result.success(new DeptEntity(10001, "部门A"));// 序列化String json = mapper.writeValueAsString(source);// 构建Type对象JavaType type = mapper.getTypeFactory().constructParametricType(Result.class, DeptEntity.class);// 反序列化Result<DeptEntity> target = mapper.readValue(json, type);System.out.println(target.getData().getClass());System.out.println(target);}
泛型嵌套的序列化
@Testvoid test() throws JsonProcessingException {String key = "res";// 重头戏来了 泛型嵌套的List集合List<Map<String, Result<DeptEntity>>> source = CollUtil.newArrayList();Map<String, Result<DeptEntity>> map = MapUtil.newHashMap();Result<DeptEntity> res = Result.success(new DeptEntity(10001, "部门A"));map.put(key, res);source.add(map);// 序列化String json = mapper.writeValueAsString(source);// 构建Type对象SimpleType stringType = SimpleType.constructUnsafe(String.class);JavaType result = mapper.getTypeFactory().constructParametricType(Result.class, DeptEntity.class);MapLikeType mapType = mapper.getTypeFactory().constructMapLikeType(HashMap.class, stringType, result);CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, mapType);// 反序列化List<Map<String, Result<DeptEntity>>> target = mapper.readValue(json, type);System.out.println(target.get(0).get(key).getData().getClass());System.out.println(target.get(0).get(key).getClass());System.out.println(target.get(0).getClass());System.out.println(target.getClass());System.out.println(target);}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- SpringBoot整合ES+Kibana
- SpringBoot框架SpEL表达式注入漏洞复现与原理分析
- 手写自定义springboot-starter,感受框架的魅力和原理
- 一 JPA入门学习集合springboot
- 2022年实时最新省市区县乡镇街道geojson行政边界数据获取方法
- 关于Springboot启动报错 Whitelabel Error Page: This application has no explicit mapping
- 七 SpringBoot - Redis 缓存
- 五 SpringBoot - Java8 新特性
- 四 SpringBoot - 整合Mybatis,逆向工程,JPA
- 一篇文章带你掌握主流办公框架——SpringBoot