title: Jackson deserialize elasticsearch long as LocalDateTime with Java 8
body: <p>We have a date field being populated with a <code>long</code> in elasticsearch index.</p>
  
  <p>The field mapping is:</p>
  
  <pre><code>@Field(type = FieldType.Date)
  @JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
  private LocalDateTime created;
  </code></pre>
  
  <p>And I use <code>Jackson</code> <code>JavaTimeModule</code> and <code>Jdk8Module</code> with this configuration:</p>
  
  <pre><code>@Bean
  public ElasticsearchOperations elasticsearchTemplate() {
     return new ElasticsearchRestTemplate(client(), new CustomEntityMapper());
  }
  
  public static class CustomEntityMapper implements EntityMapper {
  
          private final ObjectMapper objectMapper;
  
          public CustomEntityMapper() {
              //we use this so that Elasticsearch understands LocalDate and LocalDateTime objects
              objectMapper = new ObjectMapper()
                                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                                .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
                                .configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
                                .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
                                .configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
                                //MUST be registered BEFORE calling findAndRegisterModules
                                .registerModule(new JavaTimeModule())
                                .registerModule(new Jdk8Module());
              //only autodetect fields and ignore getters and setters for nonexistent fields when serializing/deserializing
              objectMapper.setVisibility(objectMapper.getSerializationConfig().getDefaultVisibilityChecker()
                              .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                              .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                              .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                              .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
              //load the other available modules as well
              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>But when I try to parse an entity in the index with a field such as:</p>
  
  <pre><code>"created" : 1563448935000
  </code></pre>
  
  <p>I get an error:</p>
  
  <pre><code>com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (VALUE_NUMBER_INT), expected VALUE_STRING: Expected array or string.
  </code></pre>
  
  <p>I think, it is possible to deserialize a <code>long</code> to a date, but I don't see what I am missing.</p>
  
  <p>If I map it to <code>Long</code> it works of course and same if the value is stored as <code>String</code> and we shape it and format properly in <code>@JsonFormat</code>. But is it possible to have <code>long-&gt;LocalDateTime</code> as well?</p>