gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:37:36 25 4
gpt4 key购买 nike

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

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 上下文:

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

如何从肥皂网络服务获取响应代码?我可以为 JaxWsProxyFactoryBean 指定拦截器,但如何将值从拦截器传递到 App.someMethod() 方法?

最佳答案

考虑以下代码:

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

关于java - 如何在soap客户端中获取Http响应代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46054221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com