java反序列化时区,Jackson使用Java 8将Elasticsearch反序列化为LocalDateTime

我们在日期字段中填充了longinlasticsearch索引。

字段映射为:

@Field(type = FieldType.Date)

@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)

private LocalDateTime created;

我使用Jackson JavaTimeModule并Jdk8Module使用以下配置:

@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 T mapToObject(String source, Class clazz) throws IOException {

return objectMapper.readValue(source, clazz);

}

}

但是,当我尝试使用以下字段解析索引中的实体时:

"created" : 1563448935000

我收到一个错误:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (VALUE_NUMBER_INT), expected VALUE_STRING: Expected array or string.

我认为,可以对long日期反序列化,但是我看不到缺少的内容。

如果我将其映射到Long它,则当然可以工作,并且如果将值存储为String,我们也可以将其成形并正确格式化@JsonFormat。但是有可能也有long->LocalDateTime吗?