环境是springboot是2.3.7,elasticsearch是8.4.3
关于8.4.3的官方文档:https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.4/installation.html
问题描述
创建ElasticsearchClient 对象:
一开始报错ClassNotFoundException: jakarta.json.spi.JsonProvider,然后看了下官方文档修改了下jakarta.json-api的版本.解决完成之后报的找不到这个类nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/util/JsonProvider。
后来参考了好多博客,最后终于解决了,写下个博客记一下
================================================================
找不到类JsonProvider
项目已经导入:
如果直接导入jakarta.json-api,会出先没有导入成功的结果:还会是默认的1.1.6版本
spring子项目依赖修改父项目依赖问题:必须在下面dependencyManagement中导入正确的依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>${jakartajson.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
其中的jakartajson.version在上面声明
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>8.4.3</elasticsearch.version>
<jackson.version>2.11.3</jackson.version>
<jakartajson.version>2.0.1</jakartajson.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
刷新,可以看到导入成功
================================================================
找不到类JacksonFeature
是因为
这两个版本号不一致的问题,按照官网推荐,这边在创建springboot项目的时候没有勾选springboot-data-elasticsearch,而在在springboot中默认版本为7.6.2。
解决办法
效仿上面,导入正确的版本8.4.3
properties标签中
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>8.4.3</elasticsearch.version>
<jackson.version>2.11.3</jackson.version>
<jakartajson.version>2.0.1</jakartajson.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
dependencyManagement标签中
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>${jakartajson.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
刷新之后,可以看到
最后
关于项目中的完整pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rui</groupId>
<artifactId>ElasticSearch_Study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ElasticSearch_Study</name>
<description>ElasticSearch_Study</description>
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>8.4.3</elasticsearch.version>
<jackson.version>2.11.3</jackson.version>
<jakartajson.version>2.0.1</jakartajson.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>${jakartajson.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.rui.ElasticSearchStudyApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
配置类ElasticSearchClientConfig
package com.rui.config;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchClientConfig {
@Bean
public ElasticsearchClient elasticsearchClient(){
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
//es 客户端
ElasticsearchClient client = new ElasticsearchClient(transport);
return client;
}
}
测试类ElasticSearchStudyApplicationTests
package com.rui;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class ElasticSearchStudyApplicationTests {
@Autowired
@Qualifier("elasticsearchClient")
private ElasticsearchClient client;
//测试索引的创建
@Test
void test1() throws IOException {
//写法比RestHighLevelClient更加简洁
CreateIndexResponse indexResponse = client.indices().create(c -> c.index("user2"));
}
}
测试成功
文章来源:https://www.toymoban.com/news/detail-786601.html
关于具体CRUD的操作,推荐博客:https://blog.csdn.net/yscjhghngh/article/details/123620860文章来源地址https://www.toymoban.com/news/detail-786601.html
到了这里,关于关于springboot整合elasticsearch8.4.3的找不到相关类JsonProvider、JsonProvider的解决方案的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!