Java 从post请求向进程传递动态文件名
上传到spring批处理后传递动态文件名 我是spring batch的新手,我正在尝试完成的是从一个应用程序上传一个csv文件,然后向spring batch发送一个带有上传文件名的post请求,并让spring batch从文件所在的位置提取文件并进行处理 我试图将字符串值传递给读取器,但不知道如何在步骤中访问它Java 从post请求向进程传递动态文件名,java,spring,spring-batch,Java,Spring,Spring Batch,上传到spring批处理后传递动态文件名 我是spring batch的新手,我正在尝试完成的是从一个应用程序上传一个csv文件,然后向spring batch发送一个带有上传文件名的post请求,并让spring batch从文件所在的位置提取文件并进行处理 我试图将字符串值传递给读取器,但不知道如何在步骤中访问它 // controller where i want to pass the file name to the reader @RestController public cla
// controller where i want to pass the file name to the reader
@RestController
public class ProcessController {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job importUserJob;
@PostMapping("/load")
public BatchStatus Load(@RequestParam("filePath") String filePath) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
JobExecution jobExecution = jobLauncher.run(importUserJob, new JobParametersBuilder()
.addString("fullPathFileName", filePath)
.toJobParameters());
return jobExecution.getStatus();
}
}
//reader in my configuration class
@Bean
public FlatFileItemReader<Person> reader(@Value("#{jobParameters[fullPathFileName]}") String pathToFile)
// the problem is here at the .reader chain
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Person, Person> chunk(10)
.reader(reader(reader))
.processor(processor())
.writer(writer())
.build();
}
//要将文件名传递给读卡器的控制器
@RestController
公共类进程控制器{
@自动连线
JobLauncher JobLauncher;
@自动连线
工作导入者工作;
@后映射(“/load”)
公共BatchStatus加载(@RequestParam(“filePath”)字符串filePath)引发JobParametersInvalidException、JobExecutionReadyRunningException、JobRestartException、JobInstanceAlreadyCompleteException{
JobExecution JobExecution=jobLauncher.run(importUserJob,new JobParametersBuilder()
.addString(“fullPathFileName”,文件路径)
.toJobParameters());
返回jobExecution.getStatus();
}
}
//我的配置类中的读卡器
@豆子
公共FlatFileItemReader读取器(@Value(“#{jobParameters[fullPathFileName]}”)字符串路径文件)
//问题在.reader链上
@豆子
公共步骤第1步(){
返回stepBuilderFactory.get(“step1”)
.chunk(10)
.读卡器(读卡器(读卡器))
.processor(处理器())
.writer(writer())
.build();
}
我希望将文件名传递给读卡器,并且spring batch可以处理它您将文件的完整路径作为作业参数正确传递。但是,为了在运行时将作业参数绑定到
pathToFile
参数,您的读取器需要在步骤范围内。之所以调用它,是因为它发生在运行时最近,而不是在配置时较早(当时我们还不知道参数值)
因此,在您的情况下,您的读者可能会是:
@Bean
@StepScope
public FlatFileItemReader<Person> reader(@Value("#{jobParameters[fullPathFileName]}") String pathToFile) {
// return reader;
}
请共享csv文件和个人类
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Person, Person> chunk(10)
.reader(reader(null))
.processor(processor())
.writer(writer())
.build();
}