# New Elasticsearch 8 Java API - Insert date time field

**URL:** <https://discuss.elastic.co/t/new-elasticsearch-8-java-api-insert-date-time-field/311351>\
**Category:** Elasticsearch\
**Created:** [August 3, 2022, 10:59pm UTC](https://discuss.elastic.co/t/new-elasticsearch-8-java-api-insert-date-time-field/311351 "2022-08-03T22:59:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zxuz111](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zxuz111/32/100222_2.png) [@zxuz111](https://discuss.elastic.co/u/zxuz111)\
**Post date:** [August 3, 2022, 10:59pm UTC](https://discuss.elastic.co/t/new-elasticsearch-8-java-api-insert-date-time-field/311351/1 "2022-08-03T22:59:04Z")

</div>

I am using the new Elasticsearch 8 Java client API to persist document that contains datetime field. Also, this is a spring boot application.  
The issue I got is, if I set a field as LocalDateTime, and try to save to Elasticsearch, it throw exception as follows:  
Java Code

```auto
// model
public class TestEntity() {
        private LocalDateTime startDatetime;
}

// service
        esClient.index(i -> i
            .index("test-entity")
            .document(testEntity)
        );

```

I got the following errors:

```auto
jakarta.json.JsonException: Jackson exception
    ......
	at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: govonca.lrc.omndmnrf.geology.textsearch.model.job.EtlJobHistory["start-datetime"])
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.13.3.jar:2.13.3]
	......

```

I added the following dependency

```auto
		<dependency>
			<groupId>com.fasterxml.jackson.datatype</groupId>
			<artifactId>jackson-datatype-jsr310</artifactId>
			<version>2.13.3</version>
		</dependency>

```

and tried add the following custom ObjectMapper, but it does not resolve the problem

```auto
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        return objectMapper;
    }

```

Any help will be greatly appreciated.  
Thanks

---

<div class="post-metadata">

**Author:** ![zxuz111](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zxuz111/32/100222_2.png) [@zxuz111](https://discuss.elastic.co/u/zxuz111)\
**Post date:** [August 4, 2022, 9:10pm UTC](https://discuss.elastic.co/t/new-elasticsearch-8-java-api-insert-date-time-field/311351/2 "2022-08-04T21:10:46Z")

</div>

I figured out how to handle this, but I am not sure this is the best way.  
**The following dependency and @Bean ARE NOT needed.**

```auto
		<dependency>
			<groupId>com.fasterxml.jackson.datatype</groupId>
			<artifactId>jackson-datatype-jsr310</artifactId>
			<version>2.13.3</version>
		</dependency>

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        return objectMapper;
    }

```

Create Json serializer and deserializer.

```auto
public class LocalDatetimeDeserializer extends JsonDeserializer<LocalDateTime> {

    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctx)
        throws IOException {
        String str = p.getText();
        try {
            return LocalDateTime.parse(str, DateUtils.DEFAULT_DATETIME_PATTERN);
        } catch (DateTimeParseException e) {
            System.err.println(e);
            return null;
        }
    }
}

public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {

    @Override
    public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider provider)
        throws IOException {
        try {
            String s = value.format(DateUtils.DEFAULT_DATETIME_PATTERN);
            gen.writeString(s);
        } catch (DateTimeParseException e) {
            System.err.println(e);
            gen.writeString("");
        }
    }
}

```

And annotate the LocalDateTime field as following:

```auto
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDatetimeDeserializer.class)
    private LocalDateTime startDatetime;

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 1, 2022, 9:11pm UTC](https://discuss.elastic.co/t/new-elasticsearch-8-java-api-insert-date-time-field/311351/3 "2022-09-01T21:11:38Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
