如何在soap客户端获取Http响应代码?

时间:2017-09-05 11:46:06

标签: java spring cxf cxf-client

有一个肥皂网服务和一个客户端。使用Apache CXF / Spring软件包。
客户端:

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();
    }
}

春天背景:

<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>

用途:

public class App {
    @Autowired
    private MyWebServiceAPI wsClient;

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

如何从soap Web服务获取响应代码? 我可以为JaxWsProxyFactoryBean指定拦截器,但是如何将拦截器中的值传递给App.someMethod()方法?

1 个答案:

答案 0 :(得分:1)

考虑以下代码:

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);  
        }

另见:

ClientProxy javadoc

Client javadoc