Java 给定一个 WSDL 文件,通过 Internet 使用 Web 服务的步骤是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2404178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Given a WSDL file, what are the steps to consume a web service over the internet?
提问by Kaddy
I have been given a WSDL file and I need to consume a web service using this WSDL file over the internet. I need to do this in Java.
我收到了一个 WSDL 文件,我需要通过 Internet 使用此 WSDL 文件来使用 Web 服务。我需要在 Java 中执行此操作。
Could someone tell me the steps for doing this? I would also appreciate some useful links.
有人可以告诉我这样做的步骤吗?我也很感激一些有用的链接。
采纳答案by Malaxeur
The first step is to generate classes that can speak to this webservice. Take a look at open source solutions such as Axis2. This will generate stubs for you to talk to the webservice in code... then it's all up to you to use this service.
第一步是生成可以与此 Web 服务对话的类。看看开源解决方案,例如Axis2。这将生成存根,供您在代码中与 web 服务对话……然后就由您来使用此服务了。
回答by Rich
I haven't done this in Java in yeeeeears, but what I did that many years ago was use a command line tool called Axis (I believe the actual command was wsdl2java). You run the command passing in the url to your wsdl as an argument, and it will generate the proxy classes that you can use in your app.
我在 yeeeeears 中还没有用 Java 做过这个,但是我多年前做的是使用一个名为 Axis 的命令行工具(我相信实际的命令是 wsdl2java)。您运行将 url 作为参数传递给 wsdl 的命令,它将生成您可以在应用程序中使用的代理类。
You should do a little research on this as opposed to just getting a spoon-fed solution, but basically you'll most likely end up with some kind of tool that will automatically generate classes for you. You use these classes to interact with the service so that essentially you don't even have to know you're interacting with a service at all.
您应该对此进行一些研究,而不是仅仅获得一个勺子喂养的解决方案,但基本上您很可能最终会得到某种可以自动为您生成类的工具。您使用这些类与服务进行交互,因此基本上您甚至根本不必知道您正在与服务进行交互。
回答by Vincent Ramdhanie
It would be helpful if you are using an IDE. For instance, in Netbeans you can follow this tutorialand do what you ask quite easily. It is similarly easy in Eclipse.
如果您使用的是 IDE,这会很有帮助。例如,在 Netbeans 中,您可以按照本教程轻松完成您的要求。在 Eclipse 中也同样容易。
Here is an Eclipse pluginfor Axis (Mentioned in other answers).
这是Axis的 Eclipse 插件(在其他答案中提到)。
回答by whiskeysierra
There is a command line tool called "wsimport" bundled with your jdk (if you use 1.6, not sure about the version though). It's similiar to wsdl2java from Apache Axis, but it generates way cleaner code (imho).
有一个名为“wsimport”的命令行工具与您的 jdk 捆绑在一起(如果您使用 1.6,但不确定版本)。它类似于 Apache Axis 中的 wsdl2java,但它生成的代码更简洁(恕我直言)。
回答by Pascal Thivent
I'd use JAX-WS (please, please, forgetAxis or Axis 2, see previousanswers) and the good news is that Java 6 includes JAX-WS RI so you already have everything required, nothing to do. In other words, just use wsimport
to generate the classes required to call the web service.
我会使用 JAX-WS(请忘记Axis 或 Axis 2,请参阅之前的答案),好消息是 Java 6 包含 JAX-WS RI,因此您已经拥有所需的一切,无需执行任何操作。换句话说,只是wsimport
用来生成调用web服务所需的类。
Example:
wsimport -p stockquote http://stockquote.xyz/quote?wsdl
This will generate the Java artifacts and compile them by importing the http://stockquote.xyz/quote?wsdl.
例子:
wsimport -p stockquote http://stockquote.xyz/quote?wsdl
这将生成 Java 工件并通过导入http://stockquote.xyz/quote?wsdl 来编译它们 。
Invoking the web service is then a matter of three linesof code (without including the initialization of WS arguments). See Creating a Simple Web Service and Client with JAX-WSin the Java EE tutorial, Getting Started with JAX-WS Web Servicesor Developing JAX-WS Web Service Clientsfor examples.
调用 Web 服务只需三行代码(不包括 WS 参数的初始化)。有关示例,请参阅Java EE 教程、JAX-WS Web 服务入门或开发 JAX-WS Web 服务客户端中的使用 JAX-WS 创建简单的 Web 服务和客户端。