标签:java spring spring-boot spring-batch
我遵循这个tutorial,我得到FlatFileParseException错误:
org.springframework.batch.item.file.FlatFileParseException: Parsing
error at line: 1 in resource=[class path resource [country.csv]],
input=[AA,Aruba]
country.csv
AA,Aruba
BB,Baruba
这是我的ItemReader方法
@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;
}
和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类中缺少默认的无参数构造函数.
您正在使用BeanWrapperFieldSetMapper
将FieldSet
映射到对象.引用setTargetType(type)
Javadoc:
An object of this type will be created from its default constructor for every call to
mapFieldSet(FieldSet)
.
因此,您需要添加默认构造函数并为属性提供相应的getter / setter.
标签:java,spring,spring-boot,spring-batch 来源: https://codeday.me/bug/20190722/1503795.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。