我正在尝试创建一个具有多个步骤的Spring Batch作业,并逐步传递对象。 为此,我使用从步骤到工作环境提升的ExecutionContext。 初次运行时,没有问题数据从一步到步都正确
在下次运行时,出现错误:
“无法反序列化执行上下文”原因:com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造java.time.OffsetDateTime
的实例(不存在创建者,如默认构造一样):无法从Object值反序列化(否基于委托人或财产的创建者)
我像这样在ItemWriter中编写上下文:
@Override
public void write(List<? extends Employee> items) throws Exception {
ExecutionContext stepContext = this.stepExecution.getExecutionContext();
List<Employee> e = new ArrayList<Employee>();
e.addAll(items);
stepContext.put("someKey",e);
}
然后使用:
在ItemReader中读取它(从另一步骤开始)@BeforeStep
public void retrieveInterstepData(StepExecution stepExecution) {
JobExecution jobExecution = stepExecution.getJobExecution();
ExecutionContext jobContext = jobExecution.getExecutionContext();
this.someObject = (List<Employee>) jobContext.get("someKey");
}
我检查spring数据库上下文,并且我的日期(LocalDate,OffsetDateTime等)存储为:
"LocalDate": {
"year": 2019,"month": "OCTOber","dayOfMonth": 30,"monthValue": 10,"era": ["java.time.chrono.IsoEra","CE"],"dayOfWeek": "WEDnesDAY","dayOfYear": 303,"leapYear": false,"chronology": {
"id": "ISO","calendarType": "iso8601"
}
}
"OffsetDateTime": {
"offset": {
"totalSeconds": 0,"id": "Z","rules": {
"fixedOffset": true,"transitionRules": ["java.util.Collections$unmodifiableRandomaccessList",[]],"transitions": ["java.util.Collections$unmodifiableRandomaccessList",[]]
}
},"year": 2019,"dayOfMonth": 28,"hour": 13,"minute": 42,"nano": 511651000,"second": 36,"dayOfWeek": "MONDAY","dayOfYear": 301
}
我想这样存储它是杰克逊的选择(我什么也没有定制) 但似乎杰克逊在下次运行时无法读取其自己的格式?!
我的存根是通过swagger的“ swagger-codegen-maven-plugin”和configOptions / datelibrary = java8生成的,因此我无法更改它们。
我尝试添加
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
还有
@PostConstruct
public void init() {
objectMapper.registerModule(new JavaTimeModule());
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
在@SpringBootApplication中 没有变化
有什么想法吗?是将日期更简单地存储为“ 2019-11-04”,还是让jackson读取其自己的格式?