java.lang.IllegalStateException:未能内省类
我正在尝试将Elasticsearch添加到我的项目中。我已经在pom.xml文件中添加了必要的依赖项。运行服务器时,出现以下错误: 请帮我解决这个问题 我还试图在eclipse中找到elasticsearch.yml文件来配置节点,但没有这样的文件。在哪里可以找到elasticsearch.yml文件 HibernateConfig.javajava.lang.IllegalStateException:未能内省类,java,spring,spring-boot,elasticsearch,Java,Spring,Spring Boot,elasticsearch,我正在尝试将Elasticsearch添加到我的项目中。我已经在pom.xml文件中添加了必要的依赖项。运行服务器时,出现以下错误: 请帮我解决这个问题 我还试图在eclipse中找到elasticsearch.yml文件来配置节点,但没有这样的文件。在哪里可以找到elasticsearch.yml文件 HibernateConfig.java package net.kzn.shoppingbackend.config; import java.net.InetAddress; import
package net.kzn.shoppingbackend.config;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableElasticsearchRepositories(basePackages = "net.kzn.shoppingbackend")
@ComponentScan(basePackages = { "net.kzn.shoppingbackend" })
@EnableTransactionManagement
public class HibernateConfig {
// change the below based on the DBMS you choose
private final static String DATABASE_URL = "jdbc:h2:tcp://localhost/~/onlineshopping";
private final static String DATABASE_DRIVER = "org.h2.Driver";
private final static String DATABASE_DIALECT = "org.hibernate.dialect.H2Dialect";
private final static String DATABASE_USERNAME = "sa";
private final static String DATABASE_PASSWORD = "";
@Value("${elasticsearch.home:/home/vidyesh/.m2/repository/org/elasticsearch/client/elasticsearch-rest-client/5.6.8}")
private String elasticsearchHome;
@Value("${elasticsearch.cluster.name:elasticsearch}")
private String clusterName;
// database bean will be available
@Bean("dataSource")
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
// providing the database connection information
dataSource.setDriverClassName(DATABASE_DRIVER);
dataSource.setUrl(DATABASE_URL);
dataSource.setUsername(DATABASE_USERNAME);
dataSource.setPassword(DATABASE_PASSWORD);
return dataSource;
}
// sessionFactory bean will be available
@Bean
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);
builder.addProperties(getHibernateProperties());
builder.scanPackages("net.kzn.shoppingbackend");
return builder.buildSessionFactory();
}
// All the hibernate properties will be returned in this method
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", DATABASE_DIALECT);
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
properties.put("hibernate.hbm2ddl.auto", "update");
return properties;
}
@Bean
public Client client() {
TransportClient client = null;
try {
final Settings elasticsearchSettings = Settings.builder()
.put("client.transport.sniff", true)
.put("path.home", elasticsearchHome)
.put("cluster.name", clusterName).build();
client = new PreBuiltTransportClient(elasticsearchSettings);
client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
return client;
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(client());
}
// transactionManager bean
@Bean
public HibernateTransactionManager geTransactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
return transactionManager;
}
}
package net.kzn.shoppingbackend.load;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import net.kzn.shoppingbackend.dto.Product;
import net.kzn.shoppingbackend.dto.Users;
import net.kzn.shoppingbackend.repository.UsersRepository;
@Component
public class Loaders {
@Autowired
ElasticsearchOperations operations;
@Autowired
UsersRepository usersRepository;
@PostConstruct
@Transactional
public void loadAll(){
operations.putMapping(Product.class);
System.out.println("Loading Data");
usersRepository.save(getData());
System.out.printf("Loading Completed");
}
private List<Users> getData() {
List<Users> userses = new ArrayList<>();
userses.add(new Users("Ajay",123L, "Accounting", 12000L));
userses.add(new Users("Jaga",1234L, "Finance", 22000L));
userses.add(new Users("Thiru",1235L, "Accounting", 12000L));
return userses;
}
}
Loaders.java
package net.kzn.shoppingbackend.config;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableElasticsearchRepositories(basePackages = "net.kzn.shoppingbackend")
@ComponentScan(basePackages = { "net.kzn.shoppingbackend" })
@EnableTransactionManagement
public class HibernateConfig {
// change the below based on the DBMS you choose
private final static String DATABASE_URL = "jdbc:h2:tcp://localhost/~/onlineshopping";
private final static String DATABASE_DRIVER = "org.h2.Driver";
private final static String DATABASE_DIALECT = "org.hibernate.dialect.H2Dialect";
private final static String DATABASE_USERNAME = "sa";
private final static String DATABASE_PASSWORD = "";
@Value("${elasticsearch.home:/home/vidyesh/.m2/repository/org/elasticsearch/client/elasticsearch-rest-client/5.6.8}")
private String elasticsearchHome;
@Value("${elasticsearch.cluster.name:elasticsearch}")
private String clusterName;
// database bean will be available
@Bean("dataSource")
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
// providing the database connection information
dataSource.setDriverClassName(DATABASE_DRIVER);
dataSource.setUrl(DATABASE_URL);
dataSource.setUsername(DATABASE_USERNAME);
dataSource.setPassword(DATABASE_PASSWORD);
return dataSource;
}
// sessionFactory bean will be available
@Bean
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);
builder.addProperties(getHibernateProperties());
builder.scanPackages("net.kzn.shoppingbackend");
return builder.buildSessionFactory();
}
// All the hibernate properties will be returned in this method
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", DATABASE_DIALECT);
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
properties.put("hibernate.hbm2ddl.auto", "update");
return properties;
}
@Bean
public Client client() {
TransportClient client = null;
try {
final Settings elasticsearchSettings = Settings.builder()
.put("client.transport.sniff", true)
.put("path.home", elasticsearchHome)
.put("cluster.name", clusterName).build();
client = new PreBuiltTransportClient(elasticsearchSettings);
client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
return client;
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(client());
}
// transactionManager bean
@Bean
public HibernateTransactionManager geTransactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
return transactionManager;
}
}
package net.kzn.shoppingbackend.load;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import net.kzn.shoppingbackend.dto.Product;
import net.kzn.shoppingbackend.dto.Users;
import net.kzn.shoppingbackend.repository.UsersRepository;
@Component
public class Loaders {
@Autowired
ElasticsearchOperations operations;
@Autowired
UsersRepository usersRepository;
@PostConstruct
@Transactional
public void loadAll(){
operations.putMapping(Product.class);
System.out.println("Loading Data");
usersRepository.save(getData());
System.out.printf("Loading Completed");
}
private List<Users> getData() {
List<Users> userses = new ArrayList<>();
userses.add(new Users("Ajay",123L, "Accounting", 12000L));
userses.add(new Users("Jaga",1234L, "Finance", 22000L));
userses.add(new Users("Thiru",1235L, "Accounting", 12000L));
return userses;
}
}
请告诉我我做错了什么?这个问题与依赖性有关吗
原因:java.lang.ClassNotFoundException:
org.springframework.data.elasticsearch.core.ElasticsearchOperations
此错误消息表示包含此类的jar不在应用程序类路径上
将spring数据elasticsearch jar添加到其中,您的错误就会消失
如果您使用的是maven,请通过以下方式将jar添加到类路径:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
org.springframework.data
)您需要将spring data elasticsearch.jar文件添加到类路径中。显示错误是因为这个小错误。您可以包括错误消息中附带的stacktrace吗?我添加了完整的错误消息。我将spring data elasticsearch jar文件添加到类路径失败,以内省类错误已消失,但新的错误显示没有任何配置的节点可用:[{传输#-1}{IQoE1_yHTfGWK_L2n5EHoQ}{127.0.0.1}{127.0.0.1:9300}]12:41:34.620“o.s.d.e.r.s.AbstractElasticSearchRepository路径正确吗?@值(${elasticsearch.home:/home/vidyesh/.m2/repository/org/elasticsearch/client/transport/6.3.2}”)@vidy you's next错误消息似乎表明您在应用程序中配置的弹性搜索服务器不存在(未启动、未位于此地址和/或未在正确的端口上侦听)。请花点时间阅读stackoverflow常见问题解答:为了使网站对每个人都可用,评论应该与他们的答案/问题相关,而不包含要解决的新问题。如果您有新问题,您应该提出新问题:)
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
+--------------+----------------------------+----------------+-------------+
| Spring Data | Spring Data Elasticsearch | Elasticsearch | Spring Boot |
+--------------+----------------------------+----------------+-------------+
| 2020.0.0 | 4.1.x | 7.9.3 | 2.3.x |
| Neumann | 4.0.x | 7.6.2 | 2.3.x |
| Moore | 3.2.x | 6.8.4 | 2.2.x |
| Lovelace | 3.1.x | 6.2.2 | 2.1.x |
| Kay | 3.0.x | 5.5.0 | 2.0.x |
| Ingalls | 2.1.x | 2.4.0 | 1.5.x |
+--------------+----------------------------+----------------+-------------+