Search code examples
jsonelasticsearchserializationspring-data-elasticsearch

Spring Data ElasticSearch JSON date format cause NullpointException


I'm using Spring Data ElasticSearch v4.2.x, my data was returned in JSON String.

Here is the problem,I tried to parse JSON string to Object,then I got "java.lang.NullPointerException".I have a Date field with these annotations,I'm sure this field is causing this problem:

    @Field(type = FieldType.Keyword, name = "created_at", format = DateFormat.basic_date_time, pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdAt;

Solution

  • You defined the type as FieldType.Keyword, you would need FieldType.Date.

    The @JsonFormat annotation is ignore by Spring Data Elasticsearch.

    And please, stop using the old java.util.Date class and use the classes from java.time instead, they are now available since Java 8.