2017-10-20 202 views
0

我在SpringBoot應用程序中創建HighEndRestClient bean時出錯。我做了一個測試「應用程序」,在那裏我檢查了我可以實例化我想要的對象,然後進行我想要的調用,現在我正在開發一個新的應用程序。NoClassDefFoundError錯誤創建RestHighLevelClient bean

我在POM

<dependency> 
     <groupId>org.elasticsearch.client</groupId> 
     <artifactId>elasticsearch-rest-client</artifactId> 
     <version>5.6.3</version> 
    </dependency> 

    <dependency> 
     <groupId>org.elasticsearch.client</groupId> 
     <artifactId>elasticsearch-rest-high-level-client</artifactId> 
     <version>5.6.3</version> 
    </dependency> 

這些依賴關係,我已經寫在配置類這個非常基本的代碼沒有做太多尚未

@Configuration 
@PropertySource(value = "classpath:application.properties") 
@EnableElasticsearchRepositories(basePackages = "com.indexbuilder.es.repo") 
public class ElasticsearchConfiguration { 
    @Value("${elasticsearch.host}") 
    private String EsHost; 

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

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


    @Bean 
    public RestClientBuilder coreBuilder() throws Exception { 
     RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http")); 
     builder.setMaxRetryTimeoutMillis(10000); 

     builder.setFailureListener(new RestClient.FailureListener() { 
      @Override 
      public void onFailure(HttpHost host) { 
       System.out.println("FAILURE !!!! FailureListener HAS WOKEN UP!!!! CREATYE A FAILURE LISTENER BEAN"); 
      } 
     }); 

     return builder; 
     } 

    @Bean 
    public RestClient restLowLevelClient() throws Exception{ 
     RestClient restClient = coreBuilder().build(); 
     return restClient; 
    } 

這工作得很好,只要我可以看到(我還沒有做過多的工作呢......)

當我添加這個(最初我傳遞RestClient bean,但現在我暫時創建一個本地對象更多clarit Y)

@Bean 
    public RestHighLevelClient restHighLevelClient() throws Exception{ 
     RestClient restClient = coreBuilder().build(); 
     RestHighLevelClient client = new RestHighLevelClient(restClient); 
     return client; 
    } 

我得到這個java.lang.NoClassDefFoundError錯誤

=========|_|==============|___/=///_/ :: Spring Boot :: (v1.5.1.RELEASE)

[WARNING] java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:527) at java.lang.Thread.run(Thread.java:745) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restHighLevelClient' defined in class path resource [com/indexbuilder/configuration/ElasticsearchConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.RestHighLevelClient]: Factory method 'restHighLevelClient' threw exception; nested exception is java.lang.NoClassDefFoundError: org/elasticsearch/action/main/MainRequest at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:598) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1140) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1034) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:525) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:744) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) at com.indexbuilder.SpringBootStartUpConfig.main(SpringBootStartUpConfig.java:84) ... 6 more Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.RestHighLevelClient]: Factory method 'restHighLevelClient' threw exception; nested exception is java.lang.NoClassDefFoundError: org/elasticsearch/action/main/MainRequest at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587) ... 24 more Caused by: java.lang.NoClassDefFoundError: org/elasticsearch/action/main/MainRequest at com.indexbuilder.configuration.ElasticsearchConfiguration.restHighLevelClient(ElasticsearchConfiguration.java:95) at com.indexbuilder.configuration.ElasticsearchConfiguration$$EnhancerBySpringCGLIB$$62f74d9a.CGLIB$restHighLevelClient$1() at com.indexbuilder.configuration.ElasticsearchConfiguration$$EnhancerBySpringCGLIB$$62f74d9a$$FastClassBySpringCGLIB$$2b29ad7b.invoke() at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) at com.indexbuilder.configuration.ElasticsearchConfiguration$$EnhancerBySpringCGLIB$$62f74d9a.restHighLevelClient() at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ... 25 more Caused by: java.lang.ClassNotFoundException: org.elasticsearch.action.main.MainRequest at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 36 more

任何人都可以點我在正確的方向?

回答

3

你可能需要的核心的依賴,以及:

<dependency> 
    <groupId>org.elasticsearch</groupId> 
    <artifactId>elasticsearch</artifactId> 
    <version>5.6.3</version> 
</dependency> 

一個NoClassDefFoundError通常是配置錯誤 - 這意味着你的代碼使用引用某個類,但類本身是不是在類路徑。在這種情況下,這可能也是相關Elasticsearch poms本身的依賴管理錯​​誤,因爲它們應該包含所需的類 - 但除了可能提出問題之外,您可以做的不多。