Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Apache HttpClient 5 has no setSSLSocketFactory on HttpClients.custom()

Errorcannot find symbol: method setSSLSocketFactory

Short answer

In Apache HttpClient 5 the TLS setup no longer sits on the client builder. You build a TLS strategy from your SSLContext, give it to PoolingHttpClientConnectionManagerBuilder with setTlsSocketStrategy, and pass that connection manager to HttpClients.custom().setConnectionManager. The older setSSLSocketFactory on the pooling builder is deprecated.

  • Applies to Apache HttpClient 5.5, Java 21, Spring Boot 3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which HttpClient generation is on the classpath. The 5.x client uses org.apache.hc.client5 packages; the old 4.x code uses org.apache.http.

    mvn dependency:tree | grep -i httpc
    grep -R "org.apache.http" --include=*.java -l src | head
  2. 2Match what you see, then jump to its fix

    1. 1The compiler reports that setSSLSocketFactory does not exist on the object returned by HttpClients.custom().4.x code that sets the SSL factory on the client builder, compiled against 5.xFix
    2. 2The code compiles but the IDE or compiler warns that setSSLSocketFactory is deprecated.A call to the deprecated setSSLSocketFactory on the connection manager builderFix

What does the error look like?

  • cannot find symbol: method setSSLSocketFactory
  • HttpClients.custom().setSSLSocketFactory() method not found

Cause 1 of 2

4.x code that sets the SSL factory on the client builder, compiled against 5.x

1Check

The 5.x migration guide moves custom SSL setup out of HttpClients.custom(). The client builder takes a connection manager, and the connection manager builder takes the TLS strategy.

grep -R -n "setSSLSocketFactory" --include=*.java src

2Fix

Create the connection manager with your SSLContext and hand it to the client builder.

SSLContext sslContext = /* your trust material, built as before */;

PoolingHttpClientConnectionManager connectionManager =
    PoolingHttpClientConnectionManagerBuilder.create()
        .setTlsSocketStrategy(ClientTlsStrategyBuilder.create()
            .setSslContext(sslContext)
            .buildClassic())
        .build();

CloseableHttpClient client = HttpClients.custom()
    .setConnectionManager(connectionManager)
    .build();

3Verify

The project compiles and an HTTPS request through the client completes.

mvn -q compile

Confirmed by hc.apache.org

Did this fix it?

Cause 2 of 2

A call to the deprecated setSSLSocketFactory on the connection manager builder

1Check

PoolingHttpClientConnectionManagerBuilder.setSSLSocketFactory is deprecated and points to setTlsSocketStrategy. The classes ClientTlsStrategyBuilder (package org.apache.hc.client5.http.ssl) and its buildClassic() method are available from 5.5.

mvn -q compile

2Fix

Replace the call with setTlsSocketStrategy as in the first cause. If buildClassic() is missing, the project is on a version older than 5.5; upgrade the httpclient5 dependency.

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

3Verify

Compile again and confirm the project builds with no call to the deprecated method.

mvn -q compile

Confirmed by hc.apache.org

Did this fix it?

How do I stop it coming back?

  • Read the migration guide for each HttpClient major version before moving code; TLS, timeouts and socket options all moved to the connection manager.
  • Keep the SSLContext creation in one small class so a change of client library touches one place.
  • Close the client when you are done with it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.