mapper接口将请求Map到jobparameters

e0uiprwp  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(221)

我有一个运行作业的组件:
目前,正在构建的作业参数是固定的,但是不同的feed应该提供它们自己的请求Map到的实现 JobParameters 所以我想出了一个通用的 Mapper 接口和一 RequestMapper 应Map的接口 MyRequestJobParameters ```
@Component
public class MyJobRunner {

private JobLauncher jobLauncher;
private JobExplorer jobExplorer;
private JobLocator jobLocator;
private RequestMapper requestMapper;

@Autowired
public MyJobRunner(JobLocator jobLocator,
                    JobExplorer jobExplorer,
                     JobLauncher jobLauncher,
                     RequestMapper requestMapper) {
    this.jobLocator = jobLocator;
    this.jobExplorer = jobExplorer;
    this.jobLauncher = jobLauncher;
    this.requestMapper = requestMapper;
}

public MyResponse run(final MyRequest request, final String id) {
    JobParameters parameters = getJobParameters(request, id);
}

private JobParameters getJobParameters(MyRequest request, String id) {
    //current
    JobParametersBuilder parameters = new JobParametersBuilder();
    parameters.addString("name", request.getValue(NAME));
    parameters.addString("date", request.getValue(DATE));
    parameters.addString(ID.getKeyName(), id);
    return parameters.toJobParameters();

   //target is replace the above with requestMapper.map(...) to transform request to JobParameters through the interface
}

}

public interface Mapper<F, T> {
T map(F from);
}

public interface RequestMapper extends Mapper {
}

问题是我有 `id` 字段,该字段也应位于jobparameters中,但不在 `MyRequest` 对象。那样的话,我应该如何设计 `RequestMapper` 然后是requestmapper的实现?我不确定是否可以将hashmap用作泛型类型:

public interface RequestMapper<Map<String, MyRequest>> extends Mapper {
}

其中key是id,value是request对象。但也许有一个更干净的方法来实现这一点。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题