在运行时覆盖或设置Web服务端点以使用wsimport生成的代码 [英] overriding or setting web service endpoint at runtime for code generated with wsimport

查看:125
本文介绍了在运行时覆盖或设置Web服务端点以使用wsimport生成的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用用 wsimport 生成的代码,是否可以覆盖服务端点而无需重新生成代码?



<我写了一个简单的java webservice,以下是步骤:


  1. 我编译java类并生成war文件

  2. 将war文件部署到我的应用服务器(tomcat)

  3. 通过URL访问WSDL,例如localhost:8080 / service / helloservice?wsdl

  4. 使用带有wsimport.bat的URL来生成客户端类,例如: wsimport http:// localhost:8080 / service / helloservice?Wsdl

  5. 我在我的客户端应用程序中使用这些类来调用服务

问题是服务部署在8080以外的端口上运行的应用服务器上,客户端和服务之间的通信永远不会发生。我想知道在客户端使用的存根中创建没有服务器和端口硬编码的存根的最佳方法是什么。

解决方案

您的客户端可以在运行时通过 BindingProvider 接口。



考虑这个JAX-WS教程。编写此代码的另一种方法是:

  HelloService service = new HelloService(); 
Hello port = service.getHelloPort();
BindingProvider bindingProvider =(BindingProvider)端口;
bindingProvider.getRequestContext()。put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
http:// foo:8086 / HelloWhatever);
String response = port.sayHello(name);

警告:我没有下载教程代码并对其进行了测试。


Using code that was generated with wsimport, can the service endpoint be overridden without having to regenerate the code?

I have written a simple java webservice, following are the steps:

  1. I compile the java class and generate a war file
  2. Deploy the war file to my app server (tomcat)
  3. Access the WSDL via the URL e.g. localhost:8080/service/helloservice?wsdl
  4. use the URL with wsimport.bat to generate client classes for example: wsimport http://localhost:8080/service/helloservice?Wsdl
  5. I use those classes in my client app to call the service

The problem is that is the service is deployed on an app server running on port other than 8080, the communication between client and service never happens. I am trying to know what is the best way to create stubs that does not have server and port hardcoded in the stub used by the client.

解决方案

Your client can set the end-point in the service "port" at runtime via the BindingProvider interface.

Consider the JAX-WS client in this JAX-WS tutorial. Another way to write this code would be:

HelloService service = new HelloService();
Hello port = service.getHelloPort();
BindingProvider bindingProvider = (BindingProvider) port;
bindingProvider.getRequestContext().put(
      BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
      "http://foo:8086/HelloWhatever");
String response = port.sayHello(name);

Caveat: I haven't downloaded the tutorial code and tested this code against it.

这篇关于在运行时覆盖或设置Web服务端点以使用wsimport生成的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆