我遵循这个tutorial https://spring.io/guides/gs/batch-processing/我得到了FlatFileParseException
error:
org.springframework.batch.item.file.FlatFileParseException:解析
行错误:资源 =[类路径资源 [country.csv]] 中的 1,
输入=[AA,阿鲁巴]
country.csv
AA,Aruba
BB,Baruba
这是我的ItemReader
method
@Bean
public ItemReader<Country> reader() {
FlatFileItemReader<Country> reader = new FlatFileItemReader<Country>();
reader.setResource(new ClassPathResource("country.csv"));
reader.setLineMapper(new DefaultLineMapper<Country>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[] { "countryCode", "countryName" });
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Country>() {{
setTargetType(Country.class);
}});
}});
return reader;
}
and Country.java
@Entity
@Table(name="Country")
public class Country {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
Long id;
@Column(name = "countryCode", nullable = false, updatable = false)
String countryCode;
@Column(name = "countryName", nullable = false, updatable = false)
String countryName;
public Country(String countryCode, String countryName) {
this.countryCode = countryCode;
this.countryName = countryName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
@Override
public String toString() {
return "countryCode: " + countryCode + ", countryName: " + countryName;
}
}
这里的问题是你缺少一个默认的无参数构造函数Country
class.
您正在使用BeanWrapperFieldSetMapper http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/item/file/mapping/BeanWrapperFieldSetMapper.html映射一个FieldSet http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/item/file/transform/FieldSet.html到一个物体。引用setTargetType(type) http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/item/file/mapping/BeanWrapperFieldSetMapper.html#setTargetType-java.lang.Class-Java文档:
每次调用时都会从其默认构造函数创建此类型的对象mapFieldSet(FieldSet)
.
因此,您需要添加默认构造函数并为属性提供相应的 getter/setter。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)