gpt4 book ai didi

java - LocalDate 序列化 csv 时出现 jackson-dataformat-csv 错误

转载 作者:行者123 更新时间:2023-12-02 01:08:25 26 4
gpt4 key购买 nike

我尝试以两种方式序列化包含 java.time.LocalDate 的对象,但都失败了。

  1. 使用 @JsonUnwrapped@JsonFormat(shape=JsonFormat.Shape.STRING) 进行注释会出现错误:

    Exception in thread "main" com.fasterxml.jackson.dataformat.csv.CsvMappingException: CSV generator does not support Object values for properties (nested Objects) (through reference chain: java.util.ArrayList[0]->models.DateRange["finish"]->java.time.LocalDate["chronology"])

@Data
@AllArgsConstructor
@NoArgsConstructor
//@JsonFormat(shape=JsonFormat.Shape.STRING)
public class DateRange {
@JsonUnwrapped
@JsonFormat(shape=JsonFormat.Shape.STRING)
private LocalDate start;
@JsonUnwrapped
@JsonFormat(shape=JsonFormat.Shape.STRING)
private LocalDate finish;
}

--------
public class CsvPractice {
public static void main(String[] args) throws JsonProcessingException {
ArrayList<DateRange> csv = Lists.newArrayList();
for (int i = 1; i < 6; i++) {
csv.add(new DateRange(LocalDate.now().plusDays(i), LocalDate.now().plusDays(i + 1)));
}
CsvMapper mapper = new CsvMapper();
// mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
CsvSchema schema = mapper.schemaFor(DateRange.class).withHeader();
System.out.println(mapper.writer(schema).writeValueAsString(csv));
}
}
  • 配置映射器 mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); 没有对象上的注释,但得到:

    Exception in thread "main" com.fasterxml.jackson.dataformat.csv.CsvMappingException: CSV generator does not support Object values for properties (nested Objects) (through reference chain: java.util.ArrayList[0]->models.DateRange["finish"])

  • @Data
    @AllArgsConstructor
    @NoArgsConstructor
    //@JsonFormat(shape=JsonFormat.Shape.STRING)
    public class DateRange {
    // @JsonUnwrapped
    // @JsonFormat(shape=JsonFormat.Shape.STRING)
    private LocalDate start;
    // @JsonUnwrapped
    // @JsonFormat(shape=JsonFormat.Shape.STRING)
    private LocalDate finish;
    }
    ---------
    public class CsvPractice {
    public static void main(String[] args) throws JsonProcessingException {
    ArrayList<DateRange> csv = Lists.newArrayList();
    for (int i = 1; i < 6; i++) {
    csv.add(new DateRange(LocalDate.now().plusDays(i), LocalDate.now().plusDays(i + 1)));
    }
    CsvMapper mapper = new CsvMapper();
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    CsvSchema schema = mapper.schemaFor(DateRange.class).withHeader();
    System.out.println(mapper.writer(schema).writeValueAsString(csv));
    }
    }

    我的 jackson 版本是:

        <dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-csv</artifactId>
    <version>2.9.8</version>
    </dependency>

    我错过了什么吗?

    引用

    最佳答案

    您的代码即将完成。您的问题是您的映射器未正确序列化 LocalDate

    要启用 Jackson 对 Java 8 日期和时间类型的序列化,您需要包含以下模块:

    https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310/2.10.1

    完成此操作后,您需要修改代码以注册 Jackson 的 JSR-310 模块,然后指示它禁用 SerializationFeature.WRITE_DATES_AS_TIMESTAMPS

    因此,基于上述内容,包含该模块,然后只需进行以下代码更改:

    CsvMapper mapper = new CsvMapper();
    mapper.findAndRegisterModules();
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

    完成后,您的预期结果应该类似于:

    finish,start
    2020-01-16,2020-01-15
    2020-01-17,2020-01-16
    2020-01-18,2020-01-17
    2020-01-19,2020-01-18
    2020-01-20,2020-01-19

    关于java - LocalDate 序列化 csv 时出现 jackson-dataformat-csv 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59730242/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com