Add JavaTimeModule to Jackson2ExecutionContextSerializer
Resolves #3952
This commit is contained in:
committed by
Mahmoud Ben Hassine
parent
34c88606b7
commit
889dee7ab1
@@ -72,6 +72,12 @@
|
||||
<version>${jackson.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.annotation</groupId>
|
||||
<artifactId>jakarta.annotation-api</artifactId>
|
||||
|
||||
@@ -54,6 +54,7 @@ import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import org.springframework.batch.core.JobParameter;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.repository.ExecutionContextSerializer;
|
||||
@@ -129,6 +130,7 @@ public class Jackson2ExecutionContextStringSerializer implements ExecutionContex
|
||||
.configure(MapperFeature.BLOCK_UNSAFE_POLYMORPHIC_BASE_TYPES, true)
|
||||
.setDefaultTyping(createTrustedDefaultTyping(trustedClassNames))
|
||||
.addModule(new JobParametersModule())
|
||||
.addModule(new JavaTimeModule())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2022 the original author or authors.
|
||||
* Copyright 2008-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,6 +21,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -212,4 +213,23 @@ class Jackson2ExecutionContextStringSerializerTests extends AbstractExecutionCon
|
||||
assertEquals(timestamp, deserializedTimestamp);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testJavaTimeLocalDateSerialization() throws IOException {
|
||||
// given
|
||||
Jackson2ExecutionContextStringSerializer serializer = new Jackson2ExecutionContextStringSerializer();
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
LocalDate now = LocalDate.now();
|
||||
map.put("now", now);
|
||||
|
||||
// when
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
serializer.serialize(map, outputStream);
|
||||
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
|
||||
Map<String, Object> deserializedContext = serializer.deserialize(inputStream);
|
||||
|
||||
// then
|
||||
LocalDate deserializedNow = (LocalDate) deserializedContext.get("now");
|
||||
assertEquals(now, deserializedNow);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user