-3

The following code compiles nicely against Spring-5.x:

import org.apache.http.client.HttpClient;

...

    private static ClientHttpRequestFactory clientHttpRequestFactory(
        int timeOut) throws Exception
    {
        HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory =
            new HttpComponentsClientHttpRequestFactory(
            httpClient());
        httpComponentsClientHttpRequestFactory.setConnectTimeout(timeOut * 10000);
        return httpComponentsClientHttpRequestFactory;
    }

    private static HttpClient httpClient() throws Exception
    {
        SSLConnectionSocketFactory sslConnectionSocketFactory = new
            SSLConnectionSocketFactory(createSSLContext());
        return HttpClients.custom().setSSLSocketFactory(
            sslConnectionSocketFactory).build();
    }

Trying to build against Spring-6.x, however, I get an error: incompatible types: org.apache.http.client.HttpClient cannot be converted to org.apache.hc.client5.http.classic.HttpClient.

With C-code, I'd just use something like #if SPRING_VERSION > 500000 -- but this is Java... Can I import a different HttpClient -- at compile-time -- depending on the version of Spring being used?

Any other suggestions -- to keep using the same code?

4
  • show full pom.xml or build.gradle Commented Jul 18, 2024 at 2:59
  • It would seem that you answered your original question. It suspicious that one if the packages mentions Http Client 5. Share a minimal reproducible example. Commented Jul 18, 2024 at 3:07
  • Well, one is supposed to use a different HttpClient with different versions of Spring framework (5 and 6). That's a given. My question is, can this same Java-code be compiled in both cases? Commented Jul 18, 2024 at 3:23
  • No you cannot. Because it simply is a totally different class. You will need different configurations. Commented Jul 18, 2024 at 6:21

1 Answer 1

1

Spring 5.x

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

Example code

import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import javax.net.ssl.SSLContext;

...
//YOUR CODE
 private static HttpClient httpClient() throws Exception
    {
        SSLConnectionSocketFactory sslConnectionSocketFactory = new
            SSLConnectionSocketFactory(createSSLContext());
        return HttpClients.custom().setSSLSocketFactory(
            sslConnectionSocketFactory).build();
    }

Spring 6.x

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

Example code

import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import javax.net.ssl.SSLContext;
...

    private static HttpClient httpClient() throws Exception
    {
        SSLConnectionSocketFactory sslConnectionSocketFactory = new
            SSLConnectionSocketFactory(createSSLContext());

        PoolingHttpClientConnectionManagerBuilder cmBuilder = PoolingHttpClientConnectionManagerBuilder.create()
                .setSSLSocketFactory(sslConnectionSocketFactory)
                .setMaxConnTotal(200)
                .setMaxConnPerRoute(20);

        return HttpClients.custom()
                .setConnectionManager(cmBuilder.build())
                .build();
        /*
        return HttpClients.custom().setSSLSocketFactory(
            sslConnectionSocketFactory).build();
         */
    }

HttpClient 5 does not have method setSSLSocketFactory

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, so depending on the Spring version my code would have to be different. Sigh... The two implementations would have to coexist in the project, and I'll need to tell maven to pick one over another depending on the profile... Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.