JAXWS — 如何更改端点地址

2022-08-31 20:46:50

如何动态更改我的 JAXWS 客户机正在使用的地址?此客户端由 wsimport 生成。


答案 1

您可以使用 BindingProvider 接口实现该目的。

JAX-WS 定制端点

/**
 * 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();

答案 2

解决了使用 Apache CXF 的问题。

只需两行代码!下面是代码段:

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

推荐