JAXWS - 如何更改端点地址

Kic*_*obo 48 java jax-ws webservice-client

如何动态更改JAXWS客户端使用的地址?该客户端由wsimport生成.

Avi*_*gal 100

您可以使用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();
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,它运作良好!CXF非常容易使用......我的意思是,谁不聪明地弄清楚我们需要使用BindingProvider接口来修改端点端点?此外,文档是如此最新的!仍然有本地conf XML文件覆盖引用的属性. (3认同)
  • 这会更改服务,缓存端口对象并根据需要多次使用它 (2认同)
  • 对此的重要说明:设置这些值后,您需要使用相同的端口对象。如果您获得一个新的,它将具有默认的 WSDL 值,并且每次调用“getPort”时都会创建一个新的值。 (2认同)

Kic*_*obo 12

使用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);
Run Code Online (Sandbox Code Playgroud)