OpenCSV没有使用注释映射JavaBeans

我使用的是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",""

我如何发现这个错误是通过源代码 . 我希望其他人也面临类似的问题,并可以就如何解决这个问题给我一个建议 . 我喜欢这种映射方法,因为它节省了我的时间和精力 . 只需将文件读入List即可完成此过程 . 这种方法的问题是我必须为JavaBeans编写工厂流程 .

欢迎提出建议 .

谢谢,

拉斯

回答(1)

3 years ago

我们在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
        }
    }

看看这会有所帮助 .