- Springboot 版本为:
2.2.5.RELEASE
,SpringCloud 版本为:Hoxton.SR3
,其中已集成了mybatis plus、redis。在集成 es 时当时引入包:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.1.3</version>
</dependency>
出现了如下异常报错信息:
java.lang.NoSuchMethodError: org.elasticsearch.client.Request.<init>(Ljava/lang/String;Ljava/lang/String;)V
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:323)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:443)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:429)
at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:368)
at com.zsh.usercenter.service.impl.LogInfoServiceImpl.getLogInfoList(LogInfoServiceImpl.java:105)
at com.zsh.usercenter.controller.LogInfoController.getLogInfoList(LogInfoController.java:45)
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.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
... 53 common frames omitted
这个问题是因为包冲突导致,简单的通过 Dependencies Analyzer
进行排除后问题依然没有解决,于是从网上百度了一下。通过其中的解决方法无法解决,于是我仔细查看 maven 引入的关于 es 具体有哪些包,从中发现如下:
发现我只想引入 elasticsearch-rest-high-level-client
6.1.3 版本,但是实际引入还引入了 elasticsearch
6.8.6 版本和 elasticsearch-rest-client
6.8.6 版本,于是通过将 elasticsearch-rest-high-level-client
中的 elasticsearch
和 elasticsearch-rest-client
排除重新引入解决问题。
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.1.3</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.1.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.1.3</version>
</dependency>
总结
对于这件事解决方法很简单,但是我花了将近一个小时去解决这问题,主要问题在于一开始思路就不对,应该去查看实际项目中引入的包,而不是一味的从百度获取解决方案,特别是对于这种场景比较复杂的包冲突,从而可以快速排查问题,节省时间。