我正在开发Spring Batch - MongoDB to XML
示例。在此示例中,我使用FlatFileItemWriter
使用以下配置写入CSV文件。
<bean id="cvsFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<!-- write to this csv file -->
<property name="resource" value="file:cvs/report.csv" />
<property name="shouldDeleteIfExists" value="true" />
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value="," />
<property name="fieldExtractor">
<!-- Extractor which returns the value of beans property through reflection -->
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="id, date, impression,clicks,earning" />
</bean>
</property>
</bean>
</property>
</bean>
我发现问题是我开发了一个使用Date
以dd-MM-yyyy
格式打印JaxbDateAdapter
public class JaxbDateAdapter extends XmlAdapter<String, Date> {
private SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
@Override
public String marshal(Date date) throws Exception {
return dateFormat.format(date);
}
@Override
public Date unmarshal(String date) throws Exception {
return dateFormat.parse(date);
}
}
的代码
@XmlRootElement(name = "record")
@XmlAccessorType(XmlAccessType.FIELD)
public class Report {
@XmlAttribute(name = "id")
private int id;
@XmlElement(name = "date")
@XmlJavaTypeAdapter(JaxbDateAdapter.class) // for date formatting
private Date date;
@XmlElement(name = "impression")
private long impression;
@XmlElement(name = "clicks")
private int clicks;
@XmlElement(name = "earning")
private BigDecimal earning;
// respective setters and Getters
}
Report.java
1,Sat Jun 01 00:00:00 IST 2013,139237,40,220.90
2,Sun Jun 02 00:00:00 IST 2013,339100,60,320.88
3,Mon Jun 03 00:00:00 IST 2013,431436,76,270.80
4,Sat Mar 12 00:00:00 IST 2016,534987,43,454.80
当程序执行时,我看到以下输出:
@XmlJavaTypeAdapter(JaxbDateAdapter.class)
显然date
似乎不会有效。如何解决这个问题?我希望dd-mm-yyyy
格式为<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="id, date, impression,clicks,earning" />
</bean>
</property>
<property name="format" value="%-9s-%tY-%tm-%td-%s-%d-%f" />
</bean>
</property>
。
根据建议,我也使用了以下配置,
> db.report.find()
{ "_id" : 1, "_class" : "com.mkyong.Report", "date" : ISODate("2013-05-31T18:30:00Z"), "impression" : NumberLong(139237), "clicks" : 40, "earning" : "220.90" }
{ "_id" : 2, "_class" : "com.mkyong.Report", "date" : ISODate("2013-06-01T18:30:00Z"), "impression" : NumberLong(339100), "clicks" : 60, "earning" : "320.88" }
{ "_id" : 3, "_class" : "com.mkyong.Report", "date" : ISODate("2013-06-02T18:30:00Z"), "impression" : NumberLong(431436), "clicks" : 76, "earning" : "270.80" }
{ "_id" : 4, "_class" : "com.mkyong.Report", "date" : ISODate("2016-03-11T18:30:00Z"), "impression" : NumberLong(534987), "clicks" : 43, "earning" : "454.80" }
由于我的mongo记录是:
java.util.IllegalFormatConversionException: Y != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302) ~[na:1.8.0_101]
at java.util.Formatter$FormatSpecifier.printDateTime(Formatter.java:2835) ~[na:1.8.0_101]
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2740) ~[na:1.8.0_101]
at java.util.Formatter.format(Formatter.java:2520) ~[na:1.8.0_101]
at java.util.Formatter.format(Formatter.java:2455) ~[na:1.8.0_101]
at java.lang.String.format(String.java:2981) ~[na:1.8.0_101]
at org.springframework.batch.item.file.transform.FormatterLineAggregator.doAggregate(FormatterLineAggregator.java:83) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.item.file.transform.ExtractorLineAggregator.aggregate(ExtractorLineAggregator.java:69) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.item.file.FlatFileItemWriter.write(FlatFileItemWriter.java:267) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProcessor.writeItems(SimpleChunkProcessor.java:175) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProcessor.doWrite(SimpleChunkProcessor.java:151) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProcessor.write(SimpleChunkProcessor.java:274) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:199) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133) ~[spring-tx-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374) [spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) [spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144) [spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) [spring-core-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at com.mkyong.model.App.main(App.java:22) [classes/:na]
但我看到以下错误,请立即指导:
<title></title>
<p><img alt="" height="364" src="http://10.11.201.93:81/webdocc/uploaded/tes3.jpg" width="496" /><img alt="" height="470" src="http://10.11.201.93:81/webdocc/uploaded/tes4.jpg" width="641" /></p>