【发布时间】:2020-06-01 12:22:23
【问题描述】:
以下是我的 WorkroomDTO:
@NotNull
private Instant createdOn;
@JsonInclude(JsonInclude.Include.NON_NULL)
private Instant changedOn;
如您所见,我正在使用 Java 8 Instant 类。 在弹性搜索索引服务器中,我将以下内容存储为 JSON:
"createdOn": {
"nano": 877000000,
"epochSecond": 1579861613
},
"changedOn": {
"nano": 920000000,
"epochSecond": 1579861613
},
问题是当我查询弹性搜索服务器以获取工作室时
return elasticsearchOperations.queryForPage(new NativeSearchQueryBuilder().withQuery(mainQuery)
.withPageable(elasticUtils.interceptPageable(searchDto.getPageable(), "name"))
.build(),
WorkroomDTO.class);
,我将这些字段映射到我的 WorkroomDTO 我得到以下异常:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.Instant` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"createdOn":{"nano":68000000,"epochSecond":1580127683}
仅供参考:
我创建了一个配置文件,其中明确地将 JavaTimeModule 注册到对象映射器
@Configuration
public class JacksonConfiguration {
@Value("${application.serialization.include-type-key:true}")
private boolean includeTypeKey = true;
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.addHandler(new MissingTypeIdHandler());
if (includeTypeKey) {
mapper.setMixInResolver(new TypeKeyMixInResolver());
}
return mapper;
}
}
需要帮助!
【问题讨论】:
标签: spring-boot elasticsearch spring-data-elasticsearch