We are migrating from Apache httpClient 4.2.6 to Apache httpClient 4.5.14. We are creating beans using bean xml in Spring boot application. Many classes and methods are deprecated. In below snippet Scheme, SSLSocketFactory and SchemeRegistryFactory are deprecated.
<beans>
<bean id="RAM.SSLSocketFactoryBean" class="org.apache.http.conn.ssl.SSLSocketFactory">
<property name="enabledSSLProtocols" value="${RAM.enabledSSLProtocols}" />
<property name="trustStoreLocation" value="${csi.adapter.projecthomepath}/${RAM.truststorelocation}" />
<property name="trustStorePassword" value="${RAM.truststore.password}" />
<property name="publicKeyAliasList" value="${RAM.publickey.alias.list}" />
<property name="enabledCiphers" value="${RAM.enabledCiphers}" />
</bean>
<bean id="RAM.Scheme" class="org.apache.http.conn.scheme.Scheme">
<constructor-arg index="0" value="https" />
<constructor-arg index="1" value="443" />
<constructor-arg index="2" ref="RAM.SSLSocketFactoryBean" />
</bean>
<bean id="RAM.SchemeRegistry" class="org.springframework.beansfactory.config.MethodInvokingFactoryBean">
<property name="targetClass">
<value>org.apache.http.impl.conn.SchemeRegistryFactory</value>
</property>
<property name="targetMethod">
<value>createDefault</value>
</property>
</bean>
<bean class="erg.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="false">
<property name="targetObject">
<ref bean="RAM.SchemeRegistry" />
</property>
<property name="targetMethod">
<value>register</value>
</property>
<property name="arguments">
<list>
<ref bean="RAM.Scheme" />
</list>
</property>
</bean>
</beans>
I dont know how to change these beans to latest httpClient 4.5.14. Please help me.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Hi there,
Migrating your Spring application’s configuration from Apache HttpClient 4.2.6 to 4.5.14 involves updating the beans to use the new classes and methods provided in the newer version.
Since Apache HttpClient 4.5.x has deprecated some of the classes like
Scheme
andSSLSocketFactory
, you need to use theSSLContextBuilder
andRegistryBuilder
for setting up SSL connections:Here’s a general idea on how to refactor your bean configuration in XML to be compatible with HttpClient 4.5.14:
SSLSocketFactory Replacement: Use
SSLContextBuilder
to create anSSLContext
, and then create anSSLConnectionSocketFactory
from theSSLContext
.Scheme and SchemeRegistry Replacement: Instead of
Scheme
andSchemeRegistry
, useRegistryBuilder
to create aRegistry<ConnectionSocketFactory>
.HttpClient Creation: Finally, create the
HttpClient
usingHttpClientBuilder
and set theRegistry<ConnectionSocketFactory>
.Here’s a revised version of your beans configuration:
In this configuration:
SSLContextBuilder
is used to create anSSLContext
.TrustSelfSignedStrategy
is an example; you may need to implement your trust strategy based on your application needs.SSLConnectionSocketFactory
is created with theSSLContext
.Registry
of connection socket factories is created.HttpClient
is built with the custom configurations.Ensure that all the placeholders like
${RAM.enabledSSLProtocols}
are correctly configured in your properties file.Also, make sure to always test thoroughly after making these changes to ensure that your application’s SSL connections are functioning as expected with the new HttpClient version.
Best,
Bobby