【发布时间】:2011-07-06 17:22:27
【问题描述】:
如何动态更改我的 JAXWS 客户端正在使用的地址? 此客户端由 wsimport 生成。
【问题讨论】:
-
动态是指在运行时吗?
标签: java jax-ws webservice-client
如何动态更改我的 JAXWS 客户端正在使用的地址? 此客户端由 wsimport 生成。
【问题讨论】:
标签: java jax-ws webservice-client
您可以使用 BindingProvider 接口来实现。
/**
* The following snippets shows how to set a custom endpoint for a JAX-WS generated WebClient on runtime
*/
// Get the service and the port
SampleService service = new SampleService();
Sample port = service.getESamplePort();
// Use the BindingProvider's context to set the endpoint
BindingProvider bp = (BindingProvider)port;
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://www.aviramsegal.com/ws/sample");
/* Optional credentials */
bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "user");
bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "password");
port.callSampleMethod();
【讨论】:
getPort 时都会创建一个新值。
使用 Apache CXF 解决了这个问题。
只需两行代码!这是sn-p:
URL url_wsdl = new URL("http://myserver/myservice?wsdl");
Service service = Service.create(url_wsdl, new QName("http://myaddress...", "ServiceName"));
return service.getPort(MyJAXWSPortClass.class);
【讨论】:
我是 PayPal 集成的新手,我不确定 Adaptive Payment api。 但我们有权使用 GetVerifiedStatus 方法检查特定电子邮件 ID 是否在 PayPal 中拥有帐户。
请使用下面的沙箱 wsdl URL 来验证电子邮件
网址:https://svcs.sandbox.paypal.com/AdaptiveAccounts?wsdl
响应将如下所示
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<ns2:GetVerifiedStatusResponse xmlns:ns2="http://svcs.paypal.com/types/aa">
<responseEnvelope>
<timestamp>2015-07-20T23:42:46.661-07:00</timestamp>
<ack>Success</ack>
<correlationId>5cea9a8575ab9</correlationId>
<build>17345626</build>
</responseEnvelope>
<accountStatus>UNVERIFIED</accountStatus>
<countryCode>IN</countryCode>
<userInfo>
<emailAddress>anandg.saga@gmail.com</emailAddress>
<accountType>PERSONAL</accountType>
<accountId>6KD7EVWM2E2AQW</accountId>
<name>
<salutation/>
<firstName>anand</firstName>
<middleName/>
<lastName>anand</lastName>
<suffix/>
</name>
<businessName/>
</userInfo>
</ns2:GetVerifiedStatusResponse>
</soapenv:Body>
</soapenv:Envelope>
注意:创建存根时不要忘记设置端点,如下所示。 如果我们不设置这个,我们就不能得到预期的输出。
String endpointURL = "https://svcs.sandbox.paypal.com/AdaptiveAccounts/GetVerifiedStatus";
使用下面的方法添加端点
private static void addEndPoint(AdaptiveAccountsPortType port,
String endpointURL) {
BindingProvider bp = (BindingProvider)port;
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
/*List hchain = bp.getBinding().getHandlerChain();
if (hchain == null) {
hchain = new ArrayList();
}
hchain.add(new HTTPUserAgentHandler());
bp.getBinding().setHandlerChain(hchain);*/
}
【讨论】:
如果您使用 wsimport,我不确定该怎么做。我遇到了同样的问题,所以我使用 Intellij IDEA(版本 9)为我创建客户端代码。它提供了一个接受 wsdl url 的服务端点构造函数。
【讨论】: