Search code examples
javaspringcxfcxf-client

How to get an Http response code in a soap client?


There is a soap web service and a client. Uses the Apache CXF / Spring bundle.
Client:

public class MyWebServiceClientFactoryCXF {
    public MyWebServiceAPI getMyWebServiceClient(String URI, String username, String password) throws MalformedURLException {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(MyWebServiceAPI.class);
        factory.setAddress(URI);
        factory.setUsername(username);
        factory.setPassword(password);
        factory.setDataBinding(new AegisDatabinding());
        return (MyWebServiceAPI) factory.create();
    }
}

Spring context:

<bean id="myWebServiceClientFactory" class="mypakg.MyWebServiceClientFactoryCXF"/>
<bean id="myWebServiceClient" factory-bean="myWebServiceClientFactory" factory-method="getMyWebServiceClient">
        <constructor-arg index="0" type="java.lang.String" value="${ws.url}"/>
        <constructor-arg index="1" type="java.lang.String" value="${ws.login}"/>
        <constructor-arg index="2" type="java.lang.String" value="${ws.pwd}"/>
</bean>

Uses:

public class App {
    @Autowired
    private MyWebServiceAPI wsClient;

    public void someMethod() {
        wsClient.getSomeInfo();
        // Need to know http response code from `wsClient.getSomeInfo()`
    }
}

How can I get the response code from the soap web service? I can specify an interceptor for JaxWsProxyFactoryBean, but how can I pass the value from interceptor to the App.someMethod() method?


Solution

  • Consider the following piece of code:

    import org.apache.cxf.message.Message;
    import org.apache.cxf.frontend.ClientProxy;
    import org.apache.cxf.endpoint.Client;
    
    
        public void someMethod() {
            try {
                wsClient.getSomeInfo(); //exception here for non-2xx http status by default
            } finally {
               Client client = ClientProxy.getClient(wsClient);
               Integer responseCode = client.getResponseContext().get(Message.RESPONSE_CODE);  
            }
    

    See also:

    ClientProxy javadoc

    Client javadoc