我尝试使用 apache camel 和 bindy 将 csv 文件转换为对象列表。
不幸的是,我收到了这个错误:
Exhausted after delivery attempt: 1 caught: java.lang.IllegalArgumentException: Some fields are missing (optional or mandatory), line : 1
java.lang.IllegalArgumentException: Some fields are missing (optional or mandatory), line : 1
这是我的课程:
主.java
public class Main {
public static void main(String args[]) throws Exception {
CamelContext context = new DefaultCamelContext();
final DataFormat bindy = new BindyCsvDataFormat("model");
context.addRoutes(new RouteBuilder() {
public void configure() {
from("http4://127.0.0.1:18080/?filename=data2.csv").unmarshal(bindy).process(new CsvFileTransformService());
}
});
context.start();
Thread.sleep(10000);
context.stop();
}
}
CSVFileTransformService.java
public class CsvFileTransformService implements Processor {
Logger log = LoggerFactory.getLogger(CsvFileTransformService.class);
@Override
public void process(Exchange exchange) throws Exception {
ArrayList<HashMap<String, Object>> list = (ArrayList) exchange.getIn()
.getBody();
for (HashMap<String, Object> map : list) {
for (String key : map.keySet()) {
log.debug("key : " + key);
CsvBean csvBean = (CsvBean) map.get(key);
log.debug("csvBean : " + csvBean);
}
}
}
}
CSVBean.java
@CsvRecord(separator = ",")
public class CsvBean implements Serializable {
@DataField(pos = 1)
private String first;
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getSecond() {
return second;
}
public void setSecond(String second) {
this.second = second;
}
@DataField(pos = 2)
private String second;
@Override
public String toString() {
return "CsvBean{" +
"first='" + first + '\'' +
", second='" + second + '\'' +
'}';
}
}
谢谢!