title: LocalDate deserialization &serialization error -jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of java.time.LocalDate
body: <p>The birthdate of the employee stored in the Elastic seems to be stored as an incorrect datatype. I have setup code for Elasticseach client with object mapper and registered JavaTime module. I am trying to pull employees list from Elasticsearch and it doesn't seem to work and is failing at deserializing. </p>
  
  <pre><code>Jar Versions:
  org.elasticsearch.client:elasticsearch-rest-client -&gt; 6.4.3 (*)
  org.elasticsearch.client:elasticsearch-rest-high-level-client -&gt; 6.4.3
  org.elasticsearch:elasticsearch -&gt; 6.4.3
  org.springframework.data:spring-data-elasticsearch -&gt; 3.1.4.RELEASE
  com.fasterxml.jackson.datatype:jackson-datatype-hppc -&gt; 2.9.8
  com.fasterxml.jackson.core:jackson-core:2.9.8
  com.fasterxml.jackson.core:jackson-databind:2.9.8 (*)
  com.fasterxml.jackson.datatype:jackson-datatype-jsr310 -&gt; 2.9.8 (*)
  com.fasterxml.jackson.datatype:jackson-datatype-hibernate5 -&gt; 2.9.8
  com.fasterxml.jackson.core:jackson-annotations -&gt; 2.9.0
  com.fasterxml.jackson.module:jackson-module-afterburner -&gt; 2.9.8
  com.fasterxml.jackson.module:jackson-module-parameter-names:2.9.8 (*)
  com.fasterxml.jackson.module:jackson-modules-java8:2.9.8
  </code></pre>
  
  <p>Build.Gradle:</p>
  
  <pre><code>compile "com.fasterxml.jackson.datatype:jackson-datatype-hppc"
      compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310"
      compile "com.fasterxml.jackson.datatype:jackson-datatype-hibernate5"
      compile "com.fasterxml.jackson.core:jackson-annotations"
      compile "com.fasterxml.jackson.core:jackson-databind"
      compile "com.fasterxml.jackson.module:jackson-module-afterburner"
      runtime group: 'com.fasterxml.jackson.module', name: 'jackson-modules-java8', version: '2.9.8', ext: 'pom'
      // https://mvnrepository.com/artifact/com.fasterxml.jackson.module/jackson-module-parameter-names
  compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-parameter-names', version: '2.9.8'
  
  compile 'org.elasticsearch:elasticsearch'
      compile 'org.elasticsearch.client:transport'
      compile 'org.elasticsearch.client:elasticsearch-rest-client'
      compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client'
  
      compile 'org.springframework.data:spring-data-elasticsearch'
  
  </code></pre>
  
  <p>application.yml</p>
  
  <pre><code>jackson:
      serialization:
           WRITE_DATES_AS_TIMESTAMPS: false
   jpa: 
  
      open-in-view: false
  </code></pre>
  
  <p>ElasticConfiguration Code:</p>
  
  <pre><code>@AutoConfigureAfter(value = { JacksonConfiguration.class })
  public class ElasticsearchConfiguration {
  
  
  
      RestHighLevelClient client = null; 
  
      @Bean
      public RestHighLevelClient buildClient() {
          return new RestHighLevelClient(
                  RestClient.builder(
                          new HttpHost("localhost", 9200, "http"),
                          new HttpHost("localhost", 9201, "http")));
      }
  
  
      @Bean
      @Primary
      public ElasticsearchOperations elasticsearchTemplate(final ElasticsearchConverter elasticsearchConverter,
              EntityMapper mapper, Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder ) {
          return new ElasticsearchTemplate((Client) buildClient(), new CustomEntityMapper(new ObjectMapper()));
      }
      //jackson2ObjectMapperBuilder.createXmlMapper(false).build()
  
  
      public class CustomEntityMapper implements EntityMapper {
  
          private ObjectMapper objectMapper;
  
          public CustomEntityMapper(ObjectMapper objectMapper) {
              this.objectMapper = objectMapper;
              objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
              objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
              objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
              objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
              //objectMapper.registerModule(new JavaTimeModule());
              JavaTimeModule javaTimeModule=new JavaTimeModule();
              // Hack time module to allow 'Z' at the end of string (i.e. javascript json's) 
              javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME));
              javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ISO_DATE));
              objectMapper.registerModule(javaTimeModule);
              objectMapper.registerModule(new ParameterNamesModule());
              objectMapper.registerModule(new Jdk8Module());
              objectMapper.findAndRegisterModules();
          }
  
          @Override
          public String mapToString(Object object) throws IOException {
              return objectMapper.writeValueAsString(object);
          }
  
          @Override
          public &lt;T&gt; T mapToObject(String source, Class&lt;T&gt; clazz) throws IOException {
              return objectMapper.readValue(source, clazz);
          }
      }
  
  }
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Caused by: org.springframework.data.elasticsearch.ElasticsearchException: failed to map source [ {"id":73,"name":"K****","birthDate":{"year":2019,"month":"MAY","dayOfYear":150,"leapYear":false,"dayOfMonth":30,"dayOfWeek":"THURSDAY","era":"CE","monthValue":5,"chronology":{"id":"ISO","calendarType":"iso8601"}},"role..}] to class Employee
      at org.springframework.data.elasticsearch.core.AbstractResultMapper.mapEntity(AbstractResultMapper.java:45) ~[spring-data-elasticsearch-3.1.4.RELEASE.jar:3.1.4.RELEASE]
      at org.springframework.data.elasticsearch.core.DefaultResultMapper.mapResults(DefaultResultMapper.java:102) ~[spring-data-elasticsearch-3.1.4.RELEASE.jar:3.1.4.RELEASE]
      at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.queryForPage(ElasticsearchTemplate.java:315) ~[spring-data-elasticsearch-3.1.4.RELEASE.jar:3.1.4.RELEASE]
      at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.queryForPage(ElasticsearchTemplate.java:309) ~[spring-data-elasticsearch-3.1.4.RELEASE.jar:3.1.4.RELEASE]
      at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.queryForPage(ElasticsearchTemplate.java:139) ~[spring-data-elasticsearch-3.1.4.RELEASE.jar:3.1.4.RELEASE]
      at org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository.findAll(AbstractElasticsearchRepository.java:126) ~[spring-data-elasticsearch-3.1.4.RELEASE.jar:3.1.4.RELEASE]
      at org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository.findAll(AbstractElasticsearchRepository.java:120) ~[spring-data-elasticsearch-3.1.4.RELEASE.jar:3.1.4.RELEASE]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_211]
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_211]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_211]
      at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_211]
      at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
      at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE]
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE]
      at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE]
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE]
      at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
  Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDate` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
   at [Source: (String)"{"id":73,"name":"Kelly","birthDate":{"year":2019,"month":"MAY","dayOfYear":150,"leapYear":false,"dayOfMonth":30,"dayOfWeek":"THURSDAY","era":"CE","monthValue":5,"chronology":{"id":"ISO","calendarType":"iso8601"}},"role...}},"lastModifiedBy""[truncated 396 chars]; line: 1, column: 38] (through reference chain: Employee["birthDate"])
      at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67) ~[jackson-databind-2.9.8.jar:2.9.8]
      at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1452) ~[jackson-databind-2.9.8.jar:2.9.8]
      at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1028) ~[jackson-databind-2.9.8.jar:2.9.8]
      at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1297) ~[jackson-databind-2.9.8.jar:2.9.8]
      at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326) ~[jackson-databind-2.9.8.jar:2.9.8]
      at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159) ~[jackson-databind-2.9.8.jar:2.9.8]
      at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127) ~[jackson-databind-2.9.8.jar:2.9.8]
      at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288) ~[jackson-databind-2.9.8.jar:2.9.8]
      at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151) ~[jackson-databind-2.9.8.jar:2.9.8]
      at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013) ~[jackson-databind-2.9.8.jar:2.9.8]
      at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004) ~[jackson-databind-2.9.8.jar:2.9.8]
      at org.springframework.data.elasticsearch.core.DefaultEntityMapper.mapToObject(DefaultEntityMapper.java:82) ~[spring-data-elasticsearch-3.1.4.RELEASE.jar:3.1.4.RELEASE]
      at org.springframework.data.elasticsearch.core.AbstractResultMapper.mapEntity(AbstractResultMapper.java:43) ~[spring-data-elasticsearch-3.1.4.RELEASE.jar:3.1.4.RELEASE]
      ... 34 common frames omitted
  </code></pre>
  
  <p>Employee:</p>
  
  <pre><code>public class Employee extends AbstractAuditingEntity implements Serializable{
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue
      private Long id;
  
      //Remaining Fields are removed for brevity  
  
  </code></pre>
  
  <p>AbstractAuditEntity</p>
  
  <pre><code>@MappedSuperclass
  @Audited
  @EntityListeners({AuditingEntityListener.class, EntityAuditEventListener.class})
  public abstract class AbstractAuditingEntity implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @CreatedBy
      @Column(name = "created_by", nullable = true, length = 50, updatable = false)
      private String createdBy;
  
      @CreatedDate
      @Column(name = "created_date")
      private LocalDateTime createdDate = LocalDateTime.now();
  
      @LastModifiedBy
      @Column(name = "last_modified_by", length = 50)
      private String lastModifiedBy;
  
      @LastModifiedDate
      @Column(name = "last_modified_date")
      private LocalDateTime lastModifiedDate = LocalDateTime.now();
  </code></pre>