gene b.
gene b.

Reputation: 11974

"Package org.apache.hc.httpclient5.conn.ssl does not exist" on Spring Boot upgrade

I had to upgrade the Spring Boot dependency 2.7.5 --> 3.0.2:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.2</version>
    <relativePath />
</parent>

The pom.xml currently references a relatively new version of HttpClient, 4.5.3:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.3</version>
    </dependency>

The code uses the org.apache.http classes which now produce the following error on Maven Build:

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;

--> incompatible types: org.apache.http.impl.client.CloseableHttpClient cannot be converted to org.apache.hc.client5.http.classic.HttpClient

To tackle this error, I replaced all occurrences of org.apache.http with org.apache.hc.httpclient5 per https://hc.apache.org/httpcomponents-client-5.2.x/migration-guide/migration-to-classic.html :

import org.apache.hc.httpclient5.conn.ssl.SSLConnectionSocketFactory;
import org.apache.hc.httpclient5.conn.ssl.TrustSelfSignedStrategy;
import org.apache.hc.httpclient5.impl.client.CloseableHttpClient;
import org.apache.hc.httpclient5.impl.client.HttpClients;
import org.apache.hc.httpclient5.ssl.SSLContextBuilder;

But now the error is package org.apache.hc.httpclient5.conn.ssl does not exist . It can't resolve the HC5 package. Any tips? enter image description here

NOTE: I also tried replacing the HttpClient dependency with the new httpclient5 dependency, but it's the same error:

<dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
    <version>5.2.1</version>
</dependency>

Upvotes: 5

Views: 16645

Answers (2)

mandroid
mandroid

Reputation: 187

I had the same problem when I tried to upgrade the spring framework version to 3.0.4 from 2.7.9, and spring-cloud-starter-vault-config:3.1.2 to spring-cloud-starter-vault-config:4.0.0.

After some digging, I found out that spring-cloud-starter-vault-config requires two other vault dependencies: spring-cloud-vault-config and spring-vault-core.

[INFO] +- org.springframework.cloud:spring-cloud-starter-vault-config:jar:4.0.0:compile
[INFO] |  +- org.apache.httpcomponents.client5:httpclient5:jar:5.1.4:compile
[INFO] |  |  \- org.apache.httpcomponents.core5:httpcore5-h2:jar:5.1.5:compile
[INFO] |  +- org.apache.httpcomponents.core5:httpcore5:jar:5.1.5:compile
[INFO] |  +- org.springframework.cloud:spring-cloud-starter:jar:3.1.5:compile
[INFO] |  |  +- org.springframework.cloud:spring-cloud-context:jar:3.1.5:compile
[INFO] |  |  +- org.springframework.cloud:spring-cloud-commons:jar:3.1.5:compile
[INFO] |  |  \- org.springframework.security:spring-security-rsa:jar:1.0.11.RELEASE:compile
[INFO] |  |     \- org.bouncycastle:bcpkix-jdk15on:jar:1.69:compile
[INFO] |  |        +- org.bouncycastle:bcprov-jdk15on:jar:1.69:compile
[INFO] |  |        \- org.bouncycastle:bcutil-jdk15on:jar:1.69:compile
[INFO] |  +- org.springframework.cloud:spring-cloud-vault-config:jar:3.1.1:compile
[INFO] |  \- org.springframework.vault:spring-vault-core:jar:2.3.2:compile

In the above dependency tree, you can see that two dependencies versions are spring-cloud-vault-config:3.1.1 and spring-vault-core:2.3.2.

I added these dependencies manually in my application pom.xml with their latest versions to resolve the issue.

spring-cloud-vault-config:4.0.0 and spring-vault-core:3.0.1.

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-vault-config</artifactId>
        <version>4.0.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.vault</groupId>
        <artifactId>spring-vault-core</artifactId>
        <version>3.0.1</version>
    </dependency>

The dependency tree after manually adding the two dependencies.

[INFO] +- org.springframework.cloud:spring-cloud-starter-vault-config:jar:4.0.0:compile
[INFO] |  +- org.apache.httpcomponents.client5:httpclient5:jar:5.1.4:compile
[INFO] |  |  \- org.apache.httpcomponents.core5:httpcore5-h2:jar:5.1.5:compile
[INFO] |  +- org.apache.httpcomponents.core5:httpcore5:jar:5.1.5:compile
[INFO] |  \- org.springframework.cloud:spring-cloud-starter:jar:3.1.5:compile
[INFO] |     +- org.springframework.cloud:spring-cloud-context:jar:3.1.5:compile
[INFO] |     +- org.springframework.cloud:spring-cloud-commons:jar:3.1.5:compile
[INFO] |     \- org.springframework.security:spring-security-rsa:jar:1.0.11.RELEASE:compile
[INFO] |        \- org.bouncycastle:bcpkix-jdk15on:jar:1.69:compile
[INFO] |           +- org.bouncycastle:bcprov-jdk15on:jar:1.69:compile
[INFO] |           \- org.bouncycastle:bcutil-jdk15on:jar:1.69:compile
[INFO] +- org.springframework.cloud:spring-cloud-vault-config:jar:4.0.0:compile
[INFO] |  \- org.springframework:spring-web:jar:6.0.6:compile
[INFO] +- org.springframework.vault:spring-vault-core:jar:3.0.1:compile
[INFO] |  +- org.springframework:spring-core:jar:6.0.6:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:6.0.6:compile
[INFO] |  +- org.springframework:spring-context:jar:6.0.6:compile
[INFO] |  \- org.springframework:spring-beans:jar:6.0.6:compile

Upvotes: 0

gene b.
gene b.

Reputation: 11974

The org.apache.hc.httpclient5. imports had to be slightly different, as follows:

import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.TrustSelfSignedStrategy;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;

rather than

import org.apache.hc.httpclient5.conn.ssl.SSLConnectionSocketFactory;
import org.apache.hc.httpclient5.conn.ssl.TrustSelfSignedStrategy;
import org.apache.hc.httpclient5.impl.client.CloseableHttpClient;
import org.apache.hc.httpclient5.impl.client.HttpClients;
import org.apache.hc.httpclient5.ssl.SSLContextBuilder;

Upvotes: 6

Related Questions