博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Data Elasticsearch 配置 LocalDate、LocalDateTime 反序列化
阅读量:4046 次
发布时间:2019-05-24

本文共 2627 字,大约阅读时间需要 8 分钟。

Spring Data Elasticsearch 使用 Date save 操作无需配置

package com.example.config;import cn.hutool.core.date.LocalDateTimeUtil;import org.elasticsearch.client.RestHighLevelClient;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.convert.converter.Converter;import org.springframework.data.elasticsearch.client.ClientConfiguration;import org.springframework.data.elasticsearch.client.RestClients;import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions;import java.time.LocalDate;import java.time.LocalDateTime;import java.util.ArrayList;import java.util.List;/** * @author 李磊 */@Configurationpublic class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
@Value("${lilei.elasticsearch.host-and-ports}") private String[] hostAndPorts; /** * 版本对应 https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.versions */ @Bean @Override public RestHighLevelClient elasticsearchClient() {
ClientConfiguration config = ClientConfiguration.builder() .connectedTo(hostAndPorts) /*.usingSsl() .withConnectTimeout(Duration.ofSeconds(5)) .withSocketTimeout(Duration.ofSeconds(3)) .withDefaultHeaders(defaultHeaders) .withBasicAuth(username, password)*/ .build(); return RestClients.create(config).rest(); } @Bean @Override public ElasticsearchCustomConversions elasticsearchCustomConversions() {
List
converters = new ArrayList<>(); converters.add(LongToLocalDateTimeConverter.INSTANCE); converters.add(LongToLocalDateConverter.INSTANCE); return new ElasticsearchCustomConversions(converters); } /** * 查询时 Long 转 LocalDateTime * * @ReadingConverter 保证仅在查询时使用 */ @ReadingConverter enum LongToLocalDateTimeConverter implements Converter
{
INSTANCE; @Override public LocalDateTime convert(Long source) {
return LocalDateTimeUtil.of(source); } } /** * 查询时 Long 转 LocalDate */ @ReadingConverter enum LongToLocalDateConverter implements Converter
{
INSTANCE; @Override public LocalDate convert(Long source) {
return LocalDateTimeUtil.of(source).toLocalDate(); } }}

转载地址:http://kfwci.baihongyu.com/

你可能感兴趣的文章
dba 常用查询
查看>>
Oracle 异机恢复
查看>>
Oracle 12C DG 搭建(RAC-RAC/RAC-单机)
查看>>
Truncate 表之恢复
查看>>
Oracle DG failover 后恢复
查看>>
mysql 主从同步配置
查看>>
为什么很多程序员都选择跳槽?
查看>>
mongdb介绍
查看>>
mongdb安装使用
查看>>
mongdb在java中的应用
查看>>
区块链技术让Yotta企业云盘为行政事业服务助力
查看>>
Yotta企业云盘更好的为媒体广告业服务
查看>>
Yotta企业云盘助力旅游行业新发展
查看>>
Yotta企业云盘助力科技行业创高峰
查看>>
Yotta企业云盘更好地为教育行业服务
查看>>
Yotta企业云盘怎么帮助到能源化工行业
查看>>
企业云盘如何助力商业新发展
查看>>
医疗行业运用企业云盘可以带来什么样的提升
查看>>
教育数字智能化能为现有体系带来新的起点
查看>>
媒体广告业如何将内容资产进行高效地综合管理与利用
查看>>