gpt4 book ai didi

java - OpenCSV 未使用注释映射 JavaBean

转载 作者:太空宇宙 更新时间:2023-11-04 12:18:16 29 4
gpt4 key购买 nike

我正在使用 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",""

我是通过查看源代码发现这个错误的。我希望其他人也面临类似的问题,并且可以给我关于如何解决这个问题的建议。我喜欢这种映射方法,因为它节省了我的时间和精力。该过程只需将文件读入列表即可进行。这种方法的问题是我必须为 JavaBean 编写工厂流程。

欢迎提出建议。

谢谢

拉斯

最佳答案

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

我们尚未确定 3.9 版本的发布日期,因此,如果您立即需要某些东西并且您已经或可以自己构建代码,请转到 BeanFieldDate 类并在转换方法中替换:

    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
}
}

看看这是否有帮助。

关于java - OpenCSV 未使用注释映射 JavaBean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39136431/

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