gpt4 book ai didi

java - 无法构造 `java.time.LocalDate` 的实例 - Spring boot、elasticsearch、jackson

转载 作者:IT老高 更新时间:2023-10-28 13:57:49 33 4
gpt4 key购买 nike

我正在使用 Spring-boot 2.0.0.M7spring-boot-starter-data-elasticsearchelasticsearch 5 和我在反序列化 LocalDate 字段时遇到错误。

我的文档是这样的:

@Document(indexName= "myIndex", type = "cluster")
public class Cluster {

@Id
@Field
private Long id;
@Field
private String name;
@Field
private ClusterUrl clusterUrl;
@Field
private ClusterVisible clusterVisible;
}

其中 ClusterVisible 是一个包含 LocalDates 的子对象:

public class ClusterVisible {

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy")
private LocalDate start;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy")
private LocalDate end;
}

所以我只查询一个集群 ID,我得到了这个异常:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDate` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"id":12345,"name":"Cluster name ","clusterName":{"de":"Cluster de","it":null,"fr":null},"clusterUrl":{"de":"/url/results","it":null,"fr":null},"clusterVisible":{"start":{"year":2017,"month":"OCTOBER","dayOfMonth":9,"dayOfWeek":"MONDAY","era":"CE","dayOfYear":282,"leapYear":false,"mo"[truncated 252 chars]; line: 1, column: 388] (through reference chain: com.example.elasticsearch5.es.cluster.model.Cluster["clusterVisible"]->com.example.elasticsearch5.es.cluster.model.ClusterVisible["start"])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1290)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)
at org.springframework.data.elasticsearch.core.DefaultEntityMapper.mapToObject(DefaultEntityMapper.java:65)

我已经知道我需要为 java.time api 添加一些 jackson 依赖项,所以我添加了:

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>

但这暂时无济于事。我还通过使用 kibana 进行查询来检查索引中的条目。查询结果为:

...
"clusterVisible": {
"start": {
"year": 2017,
"month": "OCTOBER",
"dayOfMonth": 25,
"dayOfWeek": "WEDNESDAY",
"era": "CE",
"dayOfYear": 298,
"leapYear": false,
"monthValue": 10,
"chronology": {
"id": "ISO",
"calendarType": "iso8601"
}
},
"end": {
"year": 3000,
"month": "JANUARY",
"dayOfMonth": 1,
"dayOfWeek": "WEDNESDAY",
"era": "CE",
"dayOfYear": 1,
"leapYear": false,
"monthValue": 1,
"chronology": {
"id": "ISO",
"calendarType": "iso8601"
}
}
}

修复此错误我错过了什么?

补充:确切的错误发生在 mapper.mapToObject。所以我之前创建了一个 new DefaultEntityMapper(); 一些行。会不会是这个问题?

@Override
public Page<Cluster> findClustersAndScoreByText(String text) {
QueryBuilder queryBuilder = QueryBuilders.boolQuery()
.should(QueryBuilders.queryStringQuery(text).lenient(true).defaultOperator(Operator.OR)
.field("name")
.field("svno"));

NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withQuery(queryBuilder)
.withPageable(PageRequest.of(0, 100)).build();

DefaultEntityMapper mapper = new DefaultEntityMapper();
ResultsExtractor<Page<Cluster>> rs = new ResultsExtractor<Page<Cluster>>() {

@Override
public Page<Cluster> extract(SearchResponse response) {
ArrayList<Cluster> hotels = new ArrayList<>();
SearchHit[] hits = response.getHits().getHits();
for (SearchHit hit : hits) {
try {
Cluster cluster = mapper.mapToObject(hit.getSourceAsString(), Cluster.class);
cluster.setScore(hit.getScore());
hotels.add(cluster);
} catch (IOException e) {
e.printStackTrace();
}
}
return new PageImpl<>(hotels, PageRequest.of(0, 100), response.getHits().getTotalHits());
}
};

return elasticsearchTemplate.query(nativeSearchQuery, rs);
}

最佳答案

在字段 LocalDate 上添加以下注释

@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate start;

关于java - 无法构造 `java.time.LocalDate` 的实例 - Spring boot、elasticsearch、jackson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48868034/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com