使用DSL配置SFTP出站网关时remoteDirectoryExpression缺失问题求助
解决SFTP出站网关的
remoteDirectoryExpression必填异常及动态指定远程目录问题 这个异常IllegalArgumentException: 'remoteDirectoryExpression' is required指向的问题很明确——你在配置SFTP出站网关时,缺少了远程目录的表达式配置。既然目标目录已经存在消息头中,我们可以通过SpEL表达式直接引用消息头来动态设置这个目录,具体修改方案如下:
核心修改点
在你的Sftp.outboundGateway配置中,添加.remoteDirectoryExpression()方法,用SpEL表达式从消息头中读取目录路径。假设你存储目录的消息头键名为remoteDirectory(如果实际键名不同,替换成你自己的即可),修改后的关键代码片段如下:
.handle(Sftp.outboundGateway(sftpSessionFactory, AbstractRemoteFileOutboundGateway.Command.PUT, "payload") .remoteDirectoryExpression("headers['remoteDirectory']") // 关键配置:从消息头动态获取远程目录 )
完整的batchLaunchFlow修改后代码
@Bean public IntegrationFlow batchLaunchFlow() { return IntegrationFlows.from(batchLaunchChannel()) .enrichHeaders(h -> h .headerExpression("oiCode", "payload") // 如果还未在消息头中注入远程目录,可在这里补充(根据你的实际注入逻辑调整) // .header("remoteDirectory", "/predefined/remote/path") ) .transform((GenericTransformer<String, JobLaunchRequest>) message -> { JobParameters jobParameters = new JobParametersBuilder() .addDate("exec_date", new Date()) .addString("oiCode", message) .toJobParameters(); return new JobLaunchRequest(orderServiceJob, jobParameters); }) .handle(new JobLaunchingMessageHandler(jobLauncher)) .enrichHeaders(h -> h .headerExpression("jobExecution", "payload") ) .handle((p, h) -> { // 注意:这里需要返回实际要上传的本地文件对象,不能是空File("") return new File("/local/path/to/your/target/file"); }) .handle(Sftp.outboundGateway(sftpSessionFactory, AbstractRemoteFileOutboundGateway.Command.PUT, "payload") .remoteDirectoryExpression("headers['remoteDirectory']") ) .get(); }
额外说明
remoteDirectoryExpression支持完整的SpEL语法,除了直接读取单个消息头,你还可以做动态拼接,比如根据多个字段组合目录:"headers['baseRemoteDir'] + '/' + headers['oiCode']"。- 请确保消息头中确实存在对应的目录值——可以通过日志或调试工具验证,在消息到达SFTP网关时该头字段是否存在且有效。
- 另外,你的
handle((p, h) -> { ... })部分当前返回空File(""),实际运行时必须替换为真实的本地文件路径,否则会导致上传失败。
内容的提问来源于stack exchange,提问作者Damien Walle
相关产品推荐
相关产品推荐

