背景
项目使用的
spring data Elasticsearch 5.0.1
,开始是多模块使用Jackson解析正常,在使用注解配置ES链接之后,就出现
UnrecognizedPropertyException: Unrecognized field "_class"
错误。
肯定是没有使用到注入的ObjectMapper,查询半天都没有找到原因,chatGPT也问了,无果。
只有自己跟与源码看了。
@Configuration
public class ElasticsearchConfig extends ElasticsearchConfiguration {
@Value("${spring.elasticsearch.rest.uris}")
private String elasticsearchRestUri;
@Autowired
ObjectMapper objectMapper;
@Override
public ClientConfiguration clientConfiguration() {
ClientConfiguration build = ClientConfiguration.builder()
.connectedTo(elasticsearchRestUri)
.withBasicAuth("elastic", "QLVGBDR70Im48XPalC")
.build();
return build;
}
}
这里使用的是ElasticsearchConfiguration
,进入里面可以看到默认是使用的
public abstract class ElasticsearchConfiguration extends ElasticsearchConfigurationSupport {
/**
* Provides the {@link ElasticsearchClient} to be used.
*
* @param restClient the low level RestClient to use
* @return ElasticsearchClient instance
*/
@Bean
public ElasticsearchClient elasticsearchClient(RestClient restClient) {
Assert.notNull(restClient, "restClient must not be null");