Search code examples
javaspring-bootspring-batch

Could not serialize the execution context for LOCALDATE


When I call and use it in the service, it gives the following error. I tried many things but couldn't figure it out.

Entity class

@Entity
public class User{
@Column(name = "FIRSTDATE")
@Temporal(TemporalType.DATE)
private Date firstDate;
}

Dto class

@Data
public class UserDto {
   private LocalDate firstDate;
}

dto Mapper class

UserDto dto = new UserDto();
dto.setFirstDate(DateUtil.convertToLocalDate(entity.getFirstDate()));

convertToLocalDate method

public static LocalDate convertToLocalDate(Date dateToConvert) {
        return dateToConvert == null ? null : Instant.ofEpochMilli(dateToConvert.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

When I call and use it in the service, it gives the following error. I tried many things but couldn't figure it out.

java.lang.IllegalArgumentException: Could not serialize the execution context
    at org.springframework.batch.core.repository.dao.JdbcExecutionContextDao.serializeContext(JdbcExecutionContextDao.java:306) ~[spring-batch-core-4.3.7.jar:4.3.7]
    at org.springframework.batch.core.repository.dao.JdbcExecutionContextDao.updateExecutionContext(JdbcExecutionContextDao.java:146) ~[spring-batch-core-4.3.7.jar:4.3.7]
    at org.springframework.batch.core.repository.support.SimpleJobRepository.updateExecutionContext(SimpleJobRepository.java:223) ~[spring-batch-core-4.3.7.jar:4.3.7]
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
    at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.3.23.jar:5.3.23]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.3.23.jar:5.3.23]
....
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDate` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: java.util.HashMap["userDtoList"]->java.util.ArrayList[0]->com.example.batch.data.dto.UserDto["firstDate"])
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.13.4.jar:2.13.4]
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1300) ~[jackson-databind-2.13.4.jar:2.13.4]
    at com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer.serialize(UnsupportedTypeSerializer.java:35) ~[jackson-databind-2.13.4.jar:2.13.4]

firstDate value:

2023-05-15

I added this dependeny com.fasterxml.jackson.datatype:jackson-datatype-jsr310. I couldn't solve the problem. How can I fix this problem?


Solution

  • With Spring Batch 4.3, you need to add the jackson-datatype-jsr310 dependency and configure an object mapper with the JavaTimeModule() in the Jackson2ExecutionContextStringSerializer.

    Support for the Java Date and Time API types will be added in version 5.1, see https://github.com/spring-projects/spring-batch/issues/3952.