OpenCSV 不使用注解映射 JavaBeans

问题描述 投票:0回答:1

我正在使用 OpenCSV 库的 3.8 版。我正在使用注释将 bean 字段映射到 CSV 文件中的列。我让它工作到一定程度。当日期字段为空时失败。

java.lang.RuntimeException: Error parsing CSV line: 1 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

 **Caused by: com.opencsv.exceptions.CsvDataTypeMismatchException**
at com.opencsv.bean.BeanFieldDate.convertLocaleInspecific(BeanFieldDate.java:216)
at com.opencsv.bean.BeanFieldDate.convert(BeanFieldDate.java:242)
at com.opencsv.bean.AbstractBeanField.setFieldValue(AbstractBeanField.java:70)
at com.opencsv.bean.CsvToBean.processField(CsvToBean.java:245)
at com.opencsv.bean.CsvToBean.processLine(CsvToBean.java:220)
at com.opencsv.bean.CsvToBean.processLine(CsvToBean.java:189)
at com.opencsv.bean.CsvToBean.parse(CsvToBean.java:166)
... 26 more
**Caused by: org.apache.commons.beanutils.ConversionException: No value specified for 'Date'
at** org.apache.commons.beanutils.converters.AbstractConverter.handleMissing(AbstractConverter.java:327)
at org.apache.commons.beanutils.converters.DateTimeConverter.convertToType(DateTimeConverter.java:327)
at org.apache.commons.beanutils.converters.AbstractConverter.convert(AbstractConverter.java:169)
at com.opencsv.bean.BeanFieldDate.convertLocaleInspecific(BeanFieldDate.java:214)
... 32 more

特定bean字段的注释为“required=false”,但仍然不起作用。这是我使用 Java.Util.Date 类的 JavaBean 注释:

@CsvBindByName(column="Sent On", required=true)
@CsvDate(value="MM/dd/yyyy HH:mm:ss a z")
private Date sentOn;

@CsvBindByName(column="Last Activity", required=false)
@CsvDate(value="MM/dd/yyyy HH:mm:ss a z")
private Date lastActivity;

@CsvBindByName(column="Completed On", required=false)
@CsvDate(value="MM/dd/yyyy HH:mm:ss a z")
private Date completedOn; 

这就是我创建 CSV 阅读器和解析器的方式:

    final char QUOTE_CHARACTER = '"';
    ClassLoader classLoader = getClass().getClassLoader();
    File newFile = new File(classLoader.getResource(fileName).getFile());
    CSVReader reader2 = null;

    FileReader fileReader2 = null;

    List<My_Data> myList = null;

     try {

        HeaderColumnNameMappingStrategy<My_Data> strat = new HeaderColumnNameMappingStrategy<My_Data>();
        strat.setType(My_Data.class);

        CsvToBean<My_Data> csv = new CsvToBean<>();

        final CSVParser csvParser = new CSVParserBuilder().withSeparator(delimiter)
                                                          .withQuoteChar(QUOTE_CHARACTER)
                                                          .withStrictQuotes(true)
                                                          .build();

        fileReader2 = new FileReader(newFile);

        reader2 = new CSVReaderBuilder(fileReader2).withCSVParser(csvParser)
                                                   .withVerifyReader(true)
                                                   .withKeepCarriageReturn(false)
                                                   .build();


         myList = csv.parse(strat, reader2);

这里是CSV文件数据:

"Subject","Status","Sender Name","Recipient","Sent On","Last Activity","Completed On","Completion Time (DD:HH:MM)","Created Date","Declined Date","Document ID","Reason for Declining","Expiration Date","Date Signed"
"MyCompany: Plan RCP Endorsements Select Bundle","Sent","ENV_PROD","SANDRA BULLOCK","7/21/2016 10:49:38 AM EST","7/21/2016 10:49:38 AM EST","","","7/21/2016 10:49:37 AM EST","","e05e53d8-e8bd-469e-a774-c0dec98481b2","","9/4/2016 10:49:38 AM EST",""

我是如何发现这个错误的,就是遍历源代码。我希望其他人也遇到过类似的问题,并且可以就如何解决这个问题给我一个建议。我喜欢这种映射方法,因为它可以节省我的时间和精力。该过程仅通过将文件读入列表来工作。这种方法的问题是我必须为 JavaBeans 编写工厂进程。

欢迎提出建议。

谢谢,

拉斯

java csv annotations javabeans opencsv
1个回答
0
投票

我们将您看到的问题确定为 opencsv 代码中的问题。贡献者正在重写 3.9 版本的日期处理代码,因此我们将确保该代码可以解决您的问题。

我们还没有为 3.9 版本设置日期,所以如果您立即需要一些东西并且您已经或可以自己构建代码,请转到 BeanFieldDate 类并在 convert 方法中替换:

    if (required && StringUtils.isEmpty(value)) {
        throw new CsvRequiredFieldEmptyException();
    }

这样的东西
    if (required && StringUtils.isEmpty(value)) {
        if (required) {
            throw new CsvRequiredFieldEmptyException();
        } else {
            return null;   // or maybe empty string
        }
    }

看看这是否有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.