简体   繁体   English

在运行时覆盖或设置Web服务端点以获取使用wsimport生成的代码

[英]overriding or setting web service endpoint at runtime for code generated with wsimport

Using code that was generated with wsimport , can the service endpoint be overridden without having to regenerate the code? 使用wsimport生成的代码,是否可以重写服务端点而无需重新生成代码?

I have written a simple java webservice, following are the steps: 我写了一个简单的java webservice,以下是步骤:

  1. I compile the java class and generate a war file 我编译java类并生成war文件
  2. Deploy the war file to my app server (tomcat) 将war文件部署到我的应用服务器(tomcat)
  3. Access the WSDL via the URL eg localhost:8080/service/helloservice?wsdl 通过URL访问WSDL,例如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 使用带有wsimport.bat的URL来生成客户端类,例如: 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. 问题是服务是部署在8080以外的端口上运行的应用服务器上,客户端和服务之间的通信永远不会发生。 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. 您的客户端可以在运行时通过BindingProvider接口在服务“端口”中设置端点

Consider the JAX-WS client in this JAX-WS tutorial . 这个JAX-WS教程中考虑JAX-WS客户端。 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. 警告:我没有下载教程代码并针对它测试了这段代码。

I faced the same issue, and it was terrible coz once the code is moved to production it always looked for the hardcoded WSDL location ie Windows C:........etc 我遇到了同样的问题,而且一旦代码转移到生产环节它很糟糕,它总是寻找硬编码的WSDL位置,即Windows C:........等

I have gone thru various post and pages to find the answer however all was failing then found myself a way by looking at the Service Class generated by JAX-WS imports. 我已经通过各种帖子和页面找到答案但是所有都失败了然后通过查看由JAX-WS导入生成的服务类找到了自己的方式。

I had to override the JAX-WS WSDL location implementation in my calling class like this. 我不得不像这样覆盖我的调用类中的JAX-WS WSDL位置实现。

URL baseUrl;
URL wsdlURL = null;
baseUrl = <your Services>.class.getResource(".");
try {
    wsdlURL = new URL(baseUrl, "http://<your path>?wsdl");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}

<your Services> yourServices = new <your Services(wsdlURL,new QName("your namespace", "<your service name>"));
System.out.println(Services.getWSDLDocumentLocation());
YourInterface YourInterfacePort =  yourServices.getServicePort();
BindingProvider bindingProvider = (BindingProvider)YourInterfacePort;
bindingProvider.getRequestContext().put(
          BindingProvider.ENDPOINT_ADDRESS_PROPERTY,      url);

YourInterfacePort.methods(); YourInterfacePort.methods();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是从“ wsimport”生成的Java源代码吗? <wsdl_url> 实际的肥皂网服务代码是什么? - Is the java source code generated from “wsimport <wsdl_url>” is actual soap web service code? 使用wsimport生成的Web服务客户端时获得空响应 - Getting a null response when using a wsimport generated web service client Wsimport生成的类和我的原始Web Service类中的名称冲突 - Name Collision In Wsimport Generated Class And My Original Web Service Class wsimport结果具有不同的运行时端点 - wsimport result has a different runtime endpoint 由wsimport生成的代码 - 打包代码的最佳实践 - Code generated by wsimport - best practice for packing the code Java:WSDL Web服务wsimport,我是否需要重新运行wsimport才能更改webservice服务器中的@WebService类代码 - Java: WSDL web service wsimport, do I need to re-run wsimport of I change the @WebService class code in the webservice server 具有wsimport的Java Web Service客户端未连接到主机 - Java Web Service Client With wsimport Not Connecting To Host 需要使用自定义类而不是在Web服务中生成(通过wsimport) - need to use custom classes instead of generated (by wsimport) in web-services wsimport工具在生成的客户端代码中转换DataHandler类型byte [] - wsimport tool is converting DataHandler type byte[] in the generated client code wsimport的可执行Web服务客户端时出现Java异常 - java exception when executable web service client by wsimport
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM