Skip to content

Commit 889dee7

Browse files
LichKing-leefmbenhassine
authored andcommitted
Add JavaTimeModule to Jackson2ExecutionContextSerializer
Resolves #3952
1 parent 34c8860 commit 889dee7

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

spring-batch-core/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@
7272
<version>${jackson.version}</version>
7373
<optional>true</optional>
7474
</dependency>
75+
<dependency>
76+
<groupId>com.fasterxml.jackson.datatype</groupId>
77+
<artifactId>jackson-datatype-jsr310</artifactId>
78+
<version>${jackson.version}</version>
79+
<optional>true</optional>
80+
</dependency>
7581
<dependency>
7682
<groupId>jakarta.annotation</groupId>
7783
<artifactId>jakarta.annotation-api</artifactId>

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/Jackson2ExecutionContextStringSerializer.java

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.fasterxml.jackson.databind.module.SimpleModule;
5555
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
5656

57+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
5758
import org.springframework.batch.core.JobParameter;
5859
import org.springframework.batch.core.JobParameters;
5960
import org.springframework.batch.core.repository.ExecutionContextSerializer;
@@ -129,6 +130,7 @@ public Jackson2ExecutionContextStringSerializer(String... trustedClassNames) {
129130
.configure(MapperFeature.BLOCK_UNSAFE_POLYMORPHIC_BASE_TYPES, true)
130131
.setDefaultTyping(createTrustedDefaultTyping(trustedClassNames))
131132
.addModule(new JobParametersModule())
133+
.addModule(new JavaTimeModule())
132134
.build();
133135
}
134136

spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/Jackson2ExecutionContextStringSerializerTests.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2022 the original author or authors.
2+
* Copyright 2008-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.io.InputStream;
2222
import java.sql.Timestamp;
2323
import java.time.Instant;
24+
import java.time.LocalDate;
2425
import java.util.Arrays;
2526
import java.util.HashMap;
2627
import java.util.List;
@@ -212,4 +213,23 @@ void testSqlTimestampSerialization() throws IOException {
212213
assertEquals(timestamp, deserializedTimestamp);
213214
}
214215

216+
@Test
217+
void testJavaTimeLocalDateSerialization() throws IOException {
218+
// given
219+
Jackson2ExecutionContextStringSerializer serializer = new Jackson2ExecutionContextStringSerializer();
220+
Map<String, Object> map = new HashMap<>();
221+
LocalDate now = LocalDate.now();
222+
map.put("now", now);
223+
224+
// when
225+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
226+
serializer.serialize(map, outputStream);
227+
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
228+
Map<String, Object> deserializedContext = serializer.deserialize(inputStream);
229+
230+
// then
231+
LocalDate deserializedNow = (LocalDate) deserializedContext.get("now");
232+
assertEquals(now, deserializedNow);
233+
}
234+
215235
}

0 commit comments

Comments
 (0)