Dynamically changing the address of a CXF WS Client

UPDATE: THANKS to attel in the comments below, THIS NOW WORKS. I was using the response context object, when I should have been using the request context.

I have a case where the test system is going to be different than the production system. I started with the following in my spring config file:

<!--
     Define a cxf endpoint based on client stub generated from a wsdl. It
     is important to provide serviceName and endpointName so the wsdl is
     not needed at runtime. As far as I know the serviceName and
     endpointName do not have to have a special convention but it is good
     practice to use the service namespace and Service Interface name in
     the names
 -->
<jaxws:client id="justiceXchangeService"
              serviceName="namespace:service name"
              endpointName="namespace:endpoint name"
              address="my test service address"
              serviceClass="some.package.ServiceClass">
    <jaxws:properties>
        <!--all validation is turned off-->
        <entry key="set-jaxb-validation-event-handler" value="false"/>
    </jaxws:properties>
</jaxws:client>

This would allow me to inject the client by:

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private ServiceClass serviceClass;

This worked until I had to dynamically specify the service address. I found some documentation that said I should do it like:

        BindingProvider bindingProvider = (BindingProvider) serviceClass;
        bindingProvider.getRequestContext()
                       .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "different service address");

UPDATE: THANKS to attel in the comments below, THIS NOW WORKS. I was using the response context object, when I should have been using the request context.

Unfortunately when I checked my logs, I saw that the address in the original request was still the address declared in the spring config file. Changing the binding didn’t work.

I finally ran across using a proxy. Now my spring context looks like:



This caused some changes in my code. The client is now injected via:

    @Autowired
    private JaxWsProxyFactoryBean jaxWsProxyFactoryBean;

The dynamic service is address is specifyied and the proxy used by:

        jaxWsProxyFactoryBean.setAddress("https://avsdsmv.dfa.arkansas.gov/Appriss/JXC");
        ServiceClass serviceClass = (serviceClass) jaxWsProxyFactoryBean.create();
        serviceClass.wsCall(...);
Advertisements
This entry was posted in CXF, Java, JAXB, SOAP, Spring and tagged , , , , , . Bookmark the permalink.

4 Responses to Dynamically changing the address of a CXF WS Client

  1. attel says:

    Hi,
    I think that to specify dynamically the service address you have to use the request context as in:

    BindingProvider bindingProvider = (BindingProvider) serviceClass;
    bindingProvider.getRequestContext()
    .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, “different service address”);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s