我有一个spring Boot ,其中有一个spring批处理应用程序,该应用程序处理一个输入XML文件,如您所见,XML包含一个容器元素results
和一个重复元素contact
。
<?xml version="1.0" encoding="UTF-8" ?>
<query>
<id>123</id>
<tracking>555</tracking>
<results>
<contact>
<full_name>
<first_name>John</first_name>
<last_name>Doe</last_name>
</full_name>
<street>123 Main St</street>
<city>Chicago</city>
<state>IL</state>
<zip>60610</zip>
</contact>
<contact>
<full_name>
<first_name>Jane</first_name>
<last_name>Smith</last_name>
</full_name>
<street>123 Main St</street>
<city>Miami</city>
<state>FL</state>
</contact>
</results>
</query>
beanio-configuration.xml(使用@nicoschl
建议)
<beanio xmlns="http://www.beanio.org/2012/03">
<stream name="query" format="xml" strict="true" ignoreUnidentifiedRecords="true">
<record name="id"></record>
<record name="tracking"></record>
<record name="results" class="com.test.springbatchapp.model.ContactList">
<segment name="contact" collection="list" class="com.test.springbatchapp.model.Contact" occurs="0+">
<segment name="full_name">
<field name="firstName" xmlName="first_name" maxLength="20" />
<field name="lastName" xmlName="last_name" maxLength="30" />
</segment>
<field name="street" maxLength="30" />
<field name="city" maxLength="25" />
<field name="state" minLength="2" maxLength="2" />
<field name="zip" regex="\d{5}" minOccurs="0" default="" />
</segment>
</record>
</stream>
</beanio>
Contact.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Contact {
private String firstName;
private String lastName;
private String street;
private String city;
private String state;
private String zip;
}
ContactList.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ContactList {
private List<Contact> contact;
}
读者:
@Bean
public ItemReader<Contact> reader() {
BeanIOFlatFileItemReader<Contact> reader = new BeanIOFlatFileItemReader<>();
try {
reader.setResource(new FileSystemResource(fileInputContact));
reader.setStreamName(inputContactStreamName);
reader.setStreamMapping(new ClassPathResource(beanIoConfigurationXmlPath));
reader.setStreamFactory(StreamFactory.newInstance());
reader.getLineNumber();
reader.afterPropertiesSet();
} catch (Exception e) {
log.error("ERROR: An issue occurred in the BeanIO Item Reader:: {} {}", e.getMessage(), e.getStackTrace());
}
return reader;
}
作者:
@Bean
public FlatFileItemWriter<Contact> writer() {
FlatFileItemWriter<Contact> writer = new FlatFileItemWriter<>();
writer.setResource(new FileSystemResource(fileOutputContact));
writer.setLineAggregator(new DelimitedLineAggregator<Contact>() {
{
setDelimiter(",");
setFieldExtractor(new BeanWrapperFieldExtractor<Contact>() {
{
setNames(new String[] { "firstName", "lastName", "street", "city", "state", "zip" });
}
});
}
});
return writer;
}
作业和步骤:
@Bean
public Job importUserJob(Step step1) {
return jobBuilderFactory.get("importUserJob")
.incrementer(new RunIdIncrementer())
.flow(step1)
.end()
.build();
}
@Bean
public Step step1(FlatFileItemWriter<Contact> writer) {
return stepBuilderFactory.get("step1")
.<Contact, Contact> chunk(10)
.reader(reader())
.processor(processor())
.writer(writer)
.build();
}
我遇到:在作业导入中执行步骤step1时遇到错误UserJob,com.test.springbatchapp.model.ContactList无法转换为com.test.springbatchapp.model.联系人
要解决上述错误,我需要做哪些更改?
1条答案
按热度按时间mccptt671#
可重复的不是
results
记录,因此删除occurs
属性。可重复的是您的contact
段。试试这个: