更改使用wsimport生成的Web服务客户端上的URL [英] Changing the URL on a webservice client generated with wsimport

查看:163
本文介绍了更改使用wsimport生成的Web服务客户端上的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为访问WSDL描述的Web服务的Java应用程序编写一个模块。源WSDL直接从我认为是ASP.NET Web服务下载;服务URL以.asmx扩展名结尾,在浏览器中查看服务URL显示可用于下载WSDL的链接。

I am trying to write a module for a Java application that accesses a WSDL-described webservice. The source WSDL was downloaded straight from what I believe to be an ASP.NET webservice; the service URL ends in a .asmx extension, and viewing the service URL in a browser shows a link which can be used to download the WSDL.

对我来说是一个关键要求是能够在不重新编译的情况下切换服务URL。给我的URL显然是一个测试服务器,我知道在生产中我将获得一个生产URL使用。我还希望能够自己创建一个模拟服务器进行测试,并保留将来指定新URL的功能,而无需重新编译服务。实际上,我希望安装我们的应用程序,以便能够在不同的URL实例化Web服务的多个实例。

A key requirement for me is to be able to switch out the service URL without recompiling. The URL given to me is obviously a test server and I know that in production I will be given a production URL to use. I would also like to be able to create a mock server myself for testing, and retain the ability to specify a new URL in the future without recompiling if the service is moved. In fact, I would like for an installation of our application to be able to instantiate multiple instances of the webservice at different URLs.

但是我的概念似乎与wsimport工具为我做的事情不匹配。按照f1sh的回答这里,我生成了Java

But my conception doesn't seem to match up with what the wsimport tool is doing for me. Following f1sh's answer here, I generated Java code from my downloaded WSDL with this command:

wsimport -Xnocompile -keep -b binding.xml wsdlFile.wsdl

我发现生成的代码对我下载的wsdlFile.wsdl有一个硬编码的引用,其中包含服务URL。我们的应用程序不会以通过在运行时编辑WSDL文件来配置它的方式运行。我需要在构建时将代码编译到我的应用程序中,并且可以在实例化时设置服务URL。

What I find is that the generated code has a hardcoded reference to my downloaded wsdlFile.wsdl, which contains the service URL. Our application is not going to be running in such a way that it con be configured by editing a WSDL file at runtime. I need to have code that is compiled into my application at build time and can have the service URL set at instantiation time.

我不完全确定为什么WSDL甚至需要在运行时解析;我的理解是,WSDL提供了足够的信息来生成可以访问web服务的代码,因此我不确定它为服务URL以外的生成代码提供了什么,我不确定为什么服务URL没有提供构造函数或可通过生成的Web服务类上的方法进行配置。我一定错过了什么。

I'm not entirely sure why the WSDL even needs to be parsed at runtime; it was my understanding that WSDL provides enough information to generate code which can access the webservice, so I am not sure what it is providing to the generated code other than the service URL, and I am not sure why the service URL is not provided in a constructor or configurable through a method on the generated webservice class. I must be missing something.

这种情况的一般做法是什么?大多数人会为他们将要使用的每个URL重新生成代码吗?代码是在运行时生成的吗?我可以使用另一个WSDL工具来构建具有可配置URL的客户端代码吗?

What is the general practice for this scenario? Do most people regenerate code for each individual URL that they are going to be using? Is code generated at runtime? Is there another WSDL tool I can use that will build client code with a configurable URL?

推荐答案


这种方法要求我提供一个我还不了解的javax.xml.namespace.QName对象作为第二个参数。

This approach requires me to also supply a javax.xml.namespace.QName object, which I don't yet understand, as the second argument.

从生成的源中复制一个。 QName 是一种XML 合格名称 - 唯一标识。

Copy the one from your generated source. A QName is an XML qualified name - a "unique" identity.


我仍然不明白为什么在运行时需要WSDL。

I still don't understand why the WSDL is needed at runtime.

我不能说我确切知道,但WSDL基本上是一种模式。通过提供它,我猜你给JAX-WS一个机制来验证SOAP响应。我认为JAXB绑定不足以做到这一点。

I can't say I know for sure, but a WSDL is basically a schema. By providing it, I'm guessing you give JAX-WS a mechanism to validate the SOAP response. I don't think the JAXB bindings are enough to do this.

我总是在生成的服务中使用双参数构造函数来通过 ClassLoader.getResource方法将WSDL嵌入jar中的方法。与任何模式一样,使用远程或文件系统URL是 stupid 低于最佳值。

I always use the two-argument constructor in the generated service to provide a URL via the ClassLoader.getResource method to embed the WSDL in my jar. As with any schema, using a remote or file system URL for this is stupid less than optimal.

参见关于如何在运行时设置终点的这个问题

这篇关于更改使用wsimport生成的Web服务客户端上的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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