Springboot JSON 转换:Jackson篇

2022/12/5 14:23:56

本文主要是介绍Springboot JSON 转换:Jackson篇,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

近期想整理一下 Springboot 对于处理 JSON 转换的笔记,想起了 Jackson 是 SpringMVC 默认使用的 JSON 转换器,就从 Jackson 下手,后续用到其他的在整理

本案例基于 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
@AllArgsConstructor
public class UserEntity {
    private Integer id;
    private String username;
    private String password;
    private Date birthday;
    private LocalDateTime lastLoginDate;
    private DeptEntity dept;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class DeptEntity {
    private Integer id;
    private String name;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public 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 实例

@SpringBootTest
public class SpringTest {
    @Autowired
    private ObjectMapper mapper;
}

基础类型转换

简单来说就是实体类转换,无论是实体类还是实体类嵌套方法都是一样的

实体类转换

@Test
void test() throws JsonProcessingException {
    // 实体类
    DeptEntity dept = new DeptEntity(10001, "部门A");
    // 序列化 writerWithDefaultPrettyPrinter 的作用是美化JSON
    String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(dept);
    // 反序列化
    System.out.println(mapper.readValue(json, DeptEntity.class));
}

实体类嵌套转换

@Test
void 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);
    // 序列化 writerWithDefaultPrettyPrinter 的作用是美化JSON
    String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);
    // 反序列化
    System.out.println(mapper.readValue(json, UserEntity.class));
}

集合类型转换

集合相比基础类型多个泛型的概念,需要构建类型对象进行转换,简单了解这两种集合就够了,复杂一点的后面会提到

Collection 集合转换

@Test
void 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 集合转换

@Test
void 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);
}

标签:jackson,springboot,json,构建,private,icode9 来源:

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。



这篇关于Springboot JSON 转换:Jackson篇的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程