一个项目中需要整合Elasticsearch Java API 以及Jenkins Java API。整个是整合了,最后发现调用的时候Elasticsearch API 调用 后端接口报错。
报错:Invalid receiver type interface org.apache.http.Header; not a subtype of implementation type interface org.apache.http.NameValuePair。
引入的pom依赖:
<!--elasticsearch API client -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.3.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.offbytwo.jenkins/jenkins-client -->
<!--Jenkins Java API client -->
<dependency>
<groupId>com.offbytwo.jenkins</groupId>
<artifactId>jenkins-client</artifactId>
<version>0.3.8</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
这个时候调用Elasticsearch API报错如下:
上网查看说是httpclient依赖版本与elasticsearch依赖所依赖的httpclient版本冲突。
解决办法:
打开IDEA,查看Maven依赖
我们发现引入的httpclient依赖是与jenkins所依赖的httpclient版本相同的。再查看elasticsearch-rest-high-level-client所依赖的httpclient,结果如下:
我们看到elasticsearch-rest-high-level-client需要的httpclient依赖版本要求更高,为4.5.8。
调整elasticsearch-rest-high-level-client的版本以及httpclient的版本,使得es以及jenkins API Client所依赖的httpclient兼容
因为jenkins-client的版本最高0.3.8,不能再升,所以选择降es-client版本。
httpclient版本过高jenkins-client会报错:
所以要降低elasticsearch-rest-high-level-client版本到适当程度,使得httpclient版本适中,两者所依赖的httpclient兼容。
文章验证httpclient4.5.2的版本,发现jenkins-client能够正常工作。查看elasticsearch-rest-high-level-client与httpclient的兼容版本对应发现,elasticsearch-rest-high-level-client的6.8.23所依赖的httpclient为4.5.2。所以依赖修改如下:
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<!--elasticsearch API client -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.8.23</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.8.23</version>
</dependency>
<!--jenkins Java API client -->
<dependency>
<groupId>com.offbytwo.jenkins</groupId>
<artifactId>jenkins-client</artifactId>
<version>0.3.8</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.15</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
两者得以兼容,问题解决。