I have a spring boot with a spring batch application that processes one input XML file, as you can see the XML has a container element results
and a repeating element contact
. While trying to process it I'm getting the error describe at the end:
<?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 (Using @nicoschl
suggestion)
<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;
}
Reader:
@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;
}
Writer:
@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;
}
Job and Step:
@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();
}
I'm having: Encountered an error executing step step1 in job importUserJob, com.test.springbatchapp.model.ContactList cannot be cast to com.test.springbatchapp.model.Contact
What changes do I have to make to solve above error?