我在使用jdbc-outbound-adapter中的有效负载获取值时遇到了一些问题。详情见下文
a)使用int-sftp:inbound-channel-adapter从远程服务器下载csv文件
B)。使用header-enricher
将文件名添加到标题中<int:transformer id="filetoPojoTransformer" input-channel="outputChannel" method="processContent"
output-channel="fileOutputChannel" ref="FileToPOJOTransformer" />
℃。使用自定义转换器将文件内容转换为POJO(使用openCSV库来实现此目的)
<int:transformer id="filetoPojoTransformer" input-channel="outputChannel" method="processContent"
output-channel="fileOutputChannel" ref="FileToPOJOTransformer" />
d)下面的FileToPOJOTransformer类
public class FileToPOJOTransformer {
public List processContent(File file){
String[] columns = null;
List<String> lines = null;
List<Model1> list = null;
try {
lines = Files.readAllLines(file.toPath(),
StandardCharsets.UTF_8);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
columns = lines.get(0).split(",");
final ColumnPositionMappingStrategy<Model1> strategy = new ColumnPositionMappingStrategy<>();
strategy.setType(Model1.class);
strategy.setColumnMapping(columns);
final CsvToBean<Model1> csvToBean = new CsvToBean<>();
try (final Reader reader = new FileReader(file)) {
list = csvToBean.parse(strategy, reader);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
return list;
}
}
e)根据文件名
使用路由器委派给相应的频道<int:recipient-list-router id="customRouter" input-channel="fileOutputChannel" default-output-channel="nullChannel">
<int:recipient channel="channel1" selector-expression="headers['fileName'].startsWith('File1')"/>
<int:recipient channel="channel2" selector-expression="headers['fileName'].startsWith('File2')"/>
</int:recipient-list-router>
f)现在我必须将CSV的内容存储到数据库,所以我使用了jdbc-outbound-adapter
<int-jdbc:outbound-channel-adapter
id="jdbcOutBoundAdapterEndpoint1" channel="channel1"
query="insert into ImodiumTracking.ResponsesSent (Col1)
values(:payload.get(0).Col1)"
data-source="dataSource">
</int-jdbc:outbound-channel-adapter>
我尝试了不同的选项来获取bean值,但是没有用。如何从包含POJO列表的有效负载中获取jdbc-outbound-adapter中的bean值
答案 0 :(得分:0)
List
List
拆分为将每个项目存储到数据库中?试试这个:
query="insert into ImodiumTracking.ResponsesSent (Col1) values(:payload[0].col1)"
如果您的POJO有getCol1()