Apache HttpClient 5 has no setSSLSocketFactory on HttpClients.custom()
cannot find symbol: method setSSLSocketFactoryShort 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.
Fix it fast
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 | head2Match what you see, then jump to its fix
- 1
The 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
The code compiles but the IDE or compiler warns that setSSLSocketFactory is deprecated.A call to the deprecated setSSLSocketFactory on the connection manager builderFix
- 1
What does the error look like?
cannot find symbol: method setSSLSocketFactoryHttpClients.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 src2Fix
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 compileConfirmed by hc.apache.org
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 compile2Fix
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 compileConfirmed by hc.apache.org
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
Back end
A scanner flags jackson-databind: how to upgrade it in Gradle
jackson-databind vulnerability reported by a dependency scanner
Find where the old jackson-databind comes from, then raise it.
3 causes, each with a fixBack end
Spring Boot cannot bind spring.datasource.password with Jasypt ENC() values
Failed to bind properties under 'spring.datasource.password' to java.lang.String
The binding error means Jasypt could not turn the ENC(...) value back into plain text.
3 causes, each with a fixBack end
Gradle cannot resolve spring-boot-gradle-plugin after a Spring Boot 3 upgrade
Plugin [id: 'org.springframework.boot', version: '3.0.1'] was not found in any of the following sources
Spring Boot 3 needs Java 17 or newer and a recent Gradle, and its Gradle plugin is published on the Gradle Plugin Portal.
3 causes, each with a fixBack end
bootJar cannot find the main class of a Spring Boot project
Main class name has not been configured and it could not be resolved
The Spring Boot Gradle plugin looks in the main source set's output for a class with a public static void main method.
2 causes, each with a fixBack end
Spring Boot fails at startup with an error processing a condition on an auto-configuration
java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration
This message only says Spring Boot failed while evaluating an auto-configuration condition.
3 causes, each with a fixBack end
Swagger UI URL shows the Whitelabel Error Page in Spring Boot
Whitelabel Error Page when opening /swagger-ui.html
The Whitelabel page means Spring found no handler for that address.
3 causes, each with a fix