gpt4 book ai didi

java - NoSuchMethodException : java. time.LocalDateTime.() 使用 Super CSV 读取 CSV

转载 作者:搜寻专家 更新时间:2023-11-01 01:25:04 36 4
gpt4 key购买 nike

我使用 Super CSV's 将仅包含 LocalDateTime 的实体写入 CSV 文件ICsvDozerBeanWriter,我在使用 ICsvDozerBeanReader 读回它时遇到错误。我能够成功读取和写入 Date 对象,但 LocalDateTime 不工作。

我添加了 super-csv-java8依赖和写作部分似乎工作正常。

我创建了一个小型演示应用程序 in this Github repo复制问题。运行 main() 方法,错误将输出到控制台。

这是我遇到的异常:

2016-12-09 22:24:02.427 ERROR 50405 --- [           main] org.dozer.MappingProcessor               : Field mapping error -->
MapId: null
Type: null
Source parent class: org.supercsv.io.dozer.CsvDozerBeanData
Source field name: columns
Source field type: class java.time.LocalDateTime
Source field value: 2016-12-09T22:24:02.226
Dest parent class: com.example.Entity
Dest field name: dateTime
Dest field type: java.time.LocalDateTime

org.dozer.MappingException: java.lang.NoSuchMethodException: java.time.LocalDateTime.<init>()
at org.dozer.util.MappingUtils.throwMappingException(MappingUtils.java:82) ~[dozer-5.4.0.jar:na]
at org.dozer.factory.ConstructionStrategies$ByConstructor.newInstance(ConstructionStrategies.java:261) ~[dozer-5.4.0.jar:na]
at org.dozer.factory.ConstructionStrategies$ByConstructor.create(ConstructionStrategies.java:245) ~[dozer-5.4.0.jar:na]
at org.dozer.factory.DestBeanCreator.create(DestBeanCreator.java:65) ~[dozer-5.4.0.jar:na]
at org.dozer.MappingProcessor.mapCustomObject(MappingProcessor.java:489) [dozer-5.4.0.jar:na]
at org.dozer.MappingProcessor.mapOrRecurseObject(MappingProcessor.java:446) [dozer-5.4.0.jar:na]
at org.dozer.MappingProcessor.mapFromFieldMap(MappingProcessor.java:342) [dozer-5.4.0.jar:na]
at org.dozer.MappingProcessor.mapField(MappingProcessor.java:288) [dozer-5.4.0.jar:na]
at org.dozer.MappingProcessor.map(MappingProcessor.java:248) [dozer-5.4.0.jar:na]
at org.dozer.MappingProcessor.map(MappingProcessor.java:197) [dozer-5.4.0.jar:na]
at org.dozer.MappingProcessor.map(MappingProcessor.java:187) [dozer-5.4.0.jar:na]
at org.dozer.MappingProcessor.map(MappingProcessor.java:124) [dozer-5.4.0.jar:na]
at org.dozer.MappingProcessor.map(MappingProcessor.java:119) [dozer-5.4.0.jar:na]
at org.dozer.DozerBeanMapper.map(DozerBeanMapper.java:120) [dozer-5.4.0.jar:na]
at org.supercsv.io.dozer.CsvDozerBeanReader.readIntoBean(CsvDozerBeanReader.java:220) [super-csv-dozer-2.4.0.jar:na]
at org.supercsv.io.dozer.CsvDozerBeanReader.read(CsvDozerBeanReader.java:160) [super-csv-dozer-2.4.0.jar:na]
at com.example.DemoApplication.readEntities(DemoApplication.java:51) [classes/:na]
at com.example.DemoApplication.main(DemoApplication.java:39) [classes/:na]
Caused by: java.lang.NoSuchMethodException: java.time.LocalDateTime.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_66]
at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_66]
at org.dozer.factory.ConstructionStrategies$ByConstructor.newInstance(ConstructionStrategies.java:257) ~[dozer-5.4.0.jar:na]
... 16 common frames omitted

org.dozer.MappingException: java.lang.NoSuchMethodException: java.time.LocalDateTime.<init>()
at org.dozer.util.MappingUtils.throwMappingException(MappingUtils.java:82)
at org.dozer.factory.ConstructionStrategies$ByConstructor.newInstance(ConstructionStrategies.java:261)
at org.dozer.factory.ConstructionStrategies$ByConstructor.create(ConstructionStrategies.java:245)
at org.dozer.factory.DestBeanCreator.create(DestBeanCreator.java:65)
at org.dozer.MappingProcessor.mapCustomObject(MappingProcessor.java:489)
at org.dozer.MappingProcessor.mapOrRecurseObject(MappingProcessor.java:446)
at org.dozer.MappingProcessor.mapFromFieldMap(MappingProcessor.java:342)
at org.dozer.MappingProcessor.mapField(MappingProcessor.java:288)
at org.dozer.MappingProcessor.map(MappingProcessor.java:248)
at org.dozer.MappingProcessor.map(MappingProcessor.java:197)
at org.dozer.MappingProcessor.map(MappingProcessor.java:187)
at org.dozer.MappingProcessor.map(MappingProcessor.java:124)
at org.dozer.MappingProcessor.map(MappingProcessor.java:119)
at org.dozer.DozerBeanMapper.map(DozerBeanMapper.java:120)
at org.supercsv.io.dozer.CsvDozerBeanReader.readIntoBean(CsvDozerBeanReader.java:220)
at org.supercsv.io.dozer.CsvDozerBeanReader.read(CsvDozerBeanReader.java:160)
at com.example.DemoApplication.readEntities(DemoApplication.java:51)
at com.example.DemoApplication.main(DemoApplication.java:39)
Caused by: java.lang.NoSuchMethodException: java.time.LocalDateTime.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getDeclaredConstructor(Class.java:2178)
at org.dozer.factory.ConstructionStrategies$ByConstructor.newInstance(ConstructionStrategies.java:257)

理想情况下,我想以 yyyy-MM-dd 格式将日期写入 CSV 文件,但一次一步!

最佳答案

虽然 Super CSV 确实支持通过它的 ParseLocalDateTime 读取和写入 java.time.LocalDateTimeFmtLocalDateTime单元处理器(都在 super-csv-java8 模块中可用),Dozer 试图实例化目标 LocalDateTime 对象,而不是使用单元处理器的结果(它是一个known issue 使用 Dozer - 它不支持 Java 8 时间)。

2 种解决方法是...

使用 CsvBeanReader

CsvDozerBeanReader 换成 CsvBeanReader。您将失去深度/索引映射支持,但从好的方面来说,它会快得多。

在 DozerBeanMapper 中配置 Java 8 支持

正如在 Dozer 问题上所讨论的那样,有一个 dozer-jdk8-support解决此问题的库。

添加依赖:

<dependency>
<groupId>io.craftsman</groupId>
<artifactId>dozer-jdk8-support</artifactId>
<version>1.0.2</version>
</dependency>

配置一个DozerBeanMapper:

DozerBeanMapper beanMapper = new DozerBeanMapper();
beanMapper.setMappingFiles(Collections.singletonList("dozerJdk8Converters.xml"));

并将其提供给您的CsvDozerBeanReader:

new CsvDozerBeanReader(reader, CsvPreference.STANDARD_PREFERENCE, beanMapper)

这是一些样板文件,但如果您真的需要 Dozer 支持,这将使您启动并运行。

附注我创建了一个 PR拥有 documentation已更新 - 仅列出了一个 Java 8 单元处理器,并且还有堆!

关于java - NoSuchMethodException : java. time.LocalDateTime.<init>() 使用 Super CSV 读取 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41069953/

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