Json序列化操作

前言

Jackson

序列化配置

配置1:通过 yml 文件配置日期序列化格式,只对 Date 类型字段有效,对 LocalDateTime 类型字段无效。

spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

// 原结果:TestDemoJson(id=1, name=威少, birthDate=2023-08-01T08:59:31.042, createDate=Tue Aug 01 08:59:31 CST 2023)
// json结果:{"id":1,"name":"威少","birthDate":"2023-08-01T08:59:31.042","createDate":"2023-08-01 08:59:31"}
@Test
public void testDemoJson() throws JsonProcessingException {
    TestDemoJson testDemo = new TestDemoJson();
    testDemo.setId(1);
    testDemo.setName("威少");
    testDemo.setBi	rthDate(LocalDateTime.now());
    testDemo.setCreateDate(new Date());
    String testDemoJson = objectMapper.writeValueAsString(testDemo);
    System.out.println("原结果:" + testDemo);
    System.out.println("json结果:" + testDemoJson);
}
posted @ 2023-08-08 22:46  奋斗的小猴子  阅读(51)  评论(0)    收藏  举报