首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >spring-data-elasticsearch最新版本抛出带有ElasticSearch8.1版本的NullPointer异常

spring-data-elasticsearch最新版本抛出带有ElasticSearch8.1版本的NullPointer异常
EN

Stack Overflow用户
提问于 2022-03-30 17:24:27
回答 2查看 732关注 0票数 1

中使用ElasticSearch8.1版本和'org.springframework.boot:spring-boot-starter-data-elasticsearch‘的

计划

我们的项目。Repository.save()抛出以下异常。

java.base/java.util.Objects.requireNonNull(Objects.java:221) at org.elasticsearch.action.DocWriteResponse.(DocWriteResponse.java:116) at org.elasticsearch.action.index.IndexResponse.(IndexResponse.java:43)的java.lang.NullPointerException: null

与Elasticsearch 7.15.2相同的代码运行良好。我在这里看到了支持的矩阵,https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.requirements在哪里可以看到数据插件的路线图?的8.1版本的插件支持。

Elasticsearch?提前感谢

EN

回答 2

Stack Overflow用户

发布于 2022-03-31 03:05:03

添加以下标头解决了问题。

代码语言:javascript
运行
复制
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
票数 1
EN

Stack Overflow用户

发布于 2022-07-24 12:18:40

更改用于创建客户端的代码会更好。

代码语言:javascript
运行
复制
package com.search.elasticsearchapp.config;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
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.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.http.HttpHeaders;

@Configuration
@EnableElasticsearchRepositories(basePackages = "*")
public class TestClient {

    @Value("${elasticsearch.host}")
    private String host;

    @Value("${elasticsearch.port}")
    private int port;

    @Value("${elasticsearch.protocol}")
    private String protocol;

    @Value("${elasticsearch.username}")
    private String userName;

    @Value("${elasticsearch.password}")
    private String password;

    @Bean(destroyMethod = "close")
    public RestHighLevelClient restClient() {

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));

        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, protocol))
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
                .setDefaultHeaders(compatibilityHeaders());

        return new RestHighLevelClient(builder);
    }

    private Header[] compatibilityHeaders() {
        return new Header[]{new BasicHeader(HttpHeaders.ACCEPT, "application/vnd.elasticsearch+json;compatible-with=7"), new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.elasticsearch+json;compatible-with=7")};
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71681568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档