spring boot elasticsearch 类型转换错误解决

在环境上遇到这样的报错

1
elasticsearch No converter found capable of converting from type [java.lang.Long] to type [java.time.LocalDateTime]

这是因为转换器出错了 解决办法添加转换类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

@ReadingConverter

public class LongToLocalTimeReadingConverter implements Converter<Long, LocalDateTime> {
    @Override
    public LocalDateTime convert(Long source) {
        if (source != null) {
            return Instant.ofEpochMilli(source).atZone(ZoneId.systemDefault()).toLocalDateTime();
        }
        return null;
    }
}

在 es 配置 bean 中添加进来

1
2
3
4
5
    @Bean
    @Override
    public ElasticsearchCustomConversions elasticsearchCustomConversions() {
        return new ElasticsearchCustomConversions(List.of(new LongToLocalTimeReadingConverter()));
    }

这样即可。

参考文档:

https://blog.csdn.net/qq_26834611/article/details/116980219

https://blog.csdn.net/a6694792/article/details/119421248

Licensed under CC BY-NC-SA 4.0
最后更新于 Sep 10, 2025 02:16 UTC
comments powered by Disqus
Built with Hugo
主题 StackJimmy 设计
Caret Up