类型转换异常 当前端传的long类型数据封装成为localdatatime数据类型接收时,会报错,不能直接转换 需要定义一个配置类交给spring去管理 如图所示
package com.itheima.search.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//https://blog.csdn.net/qq_33302985/article/details/109059044
//https://docs.spring.io/spring-data/elasticsearch/docs/4.2.1/reference/html/#elasticsearch.mapping.meta-model.conversions
//转换器配置
@Configuration
public class ElasticsearchConfiguration extends ElasticsearchConfigurationSupport {
@Bean
@Override
@Primary
public ElasticsearchCustomConversions elasticsearchCustomConversions() {
List converters = new ArrayList<>();
converters.add(DateToLocalDateTimeConverter.INSTANCE);
converters.add(StringToLocalDateTimeConverter.INSTANCE);
converters.add(LongToLocalDateTimeConverter.INSTANCE);
return new ElasticsearchCustomConversions(converters);
}
//long类型转成时间类型
@ReadingConverter
enum LongToLocalDateTimeConverter implements Converter {
INSTANCE;
@Override
public LocalDateTime convert(Long source) {
return Instant.ofEpochMilli(source).atZone(ZoneId.systemDefault()).toLocalDateTime();
}
}
//strig类型转成时间类型
@ReadingConverter
enum StringToLocalDateTimeConverter implements Converter {
INSTANCE;
@Override
public LocalDateTime convert(String source) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return LocalDateTime.parse(source, df);
}
}
//写将日期类型转换成时间类型
@WritingConverter
enum DateToLocalDateTimeConverter implements Converter {
INSTANCE;
@Override
public LocalDateTime convert(Date date) {
Instant instant = date.toInstant();
return instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
}
}
} 

![No converter found capable of converting from type [java.lang.Long] to type [java.time.LocalDateTime No converter found capable of converting from type [java.lang.Long] to type [java.time.LocalDateTime](http://www.mshxw.com/aiimages/31/874472.png)
