问题描述
我正在按照建议将RestHightCLient与ElasticsearchRestTemplate一起使用。我正在尝试从elasticsearch检索zoneddatetime,但似乎spring-data-elasticsearch无法创建类zoneddatetime的对象。下面的代码段有什么问题,我还需要添加其他哪些配置。
此外,如果我要直接使用RestHighLevelClient而不使用ElasticsearchOperations,那我需要做什么。
@Bean
@Override
public RestHighLevelClient elasticsearchClient() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("content-type","application/json");
ClientConfiguration clientConfiguration = ClientConfiguration
.builder()
.connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
@Bean(name = {"elasticsearchTemplate"})
public ElasticsearchOperations elasticsearhTemplate() {
return new ElasticsearchRestTemplate(elasticsearchClient());
}
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing Failed; nested exception is org.springframework.data.elasticsearch.core.convert.ConversionException: Could not create object of class java.time.zoneddatetime] with root cause
java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {YearOfEra=2020,DayOfMonth=25,OffsetSeconds=3600,MonthOfYear=8},ISO resolved to 16:46:47.891 of type java.time.format.Parsed
at java.base/java.time.LocalDate.from(LocalDate.java:397) ~[na:na]
public class XXClass {
....
@Field(type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS Z")
private zoneddatetime created = zoneddatetime.Now();
}
解决方法
更改模式以使用 uuuu 代替 yyyy ; is documented here,Elasticsearch中的更改对此负责:https://www.elastic.co/guide/en/elasticsearch/reference/current/migrate-to-java-time.html#java-time-migration-incompatible-date-formats
顺便说一句,您不需要
httpHeaders.add("content-type","application/json");
Spring Data Elasticsearch不需要@Json...
批注。