如何从 Java 调用 Web 服务(由 wsdl 描述)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1826379/
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
How to call a web service (described by a wsdl) from java
提问by codekitty
Knowing nothing of web services, I'm just trying to call some "isAlive" service that is described by a wsdl.
对网络服务一无所知,我只是想调用一些由 wsdl 描述的“isAlive”服务。
This seems to me like something that should take no more than 2-5 lines of code but I can't seem to find anything but huge long examples involving 3rd party packages etc.
在我看来,这似乎需要不超过 2-5 行代码,但除了涉及 3rd 方包等的巨大长示例之外,我似乎找不到任何东西。
Anyone has any ideas? If it is always suppose to be long maybe a good explanation as to why it has to be so complicated will also be appreciated. I'm using Eclipse and the wsdl is SOAP.
任何人有任何想法?如果它总是被认为很长,那么也可以很好地解释为什么它必须如此复杂。我正在使用 Eclipse,而 wsdl 是 SOAP。
回答by JCasso
There are plugins for IDE's which generate the needed code to consume a web service for you.
IDE 的插件可以生成所需的代码来为您使用 Web 服务。
After the plugin generates you the base methods you simply call a web service like that:
在插件为您生成基本方法后,您只需调用这样的 Web 服务:
TransportServiceSoap service = new TransportServiceLocator().getTransportServiceSoap();
service.getCities();
Have a look at http://urbas.tk/index.php/2009/02/20/eclipse-plug-in-as-a-web-service-client/
看看http://urbas.tk/index.php/2009/02/20/eclipse-plug-in-as-a-web-service-client/
回答by nos
JDK 6 comes with jax-ws, everything you need to develop a client for a web service.
JDK 6 附带了 jax-ws,它提供了为 Web 服务开发客户端所需的一切。
I'm unable to find some simple enough examples to post , but start at https://jax-ws.dev.java.net/
我找不到一些足够简单的例子来发布,但从https://jax-ws.dev.java.net/开始
Edit: here's a simple example - a client for this web service: http://xmethods.com/ve2/ViewListing.po?key=427565
编辑:这是一个简单的例子 - 这个网络服务的客户端:http: //xmethods.com/ve2/ViewListing.po?key=427565
C:\temp> md generated
C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\wsimport -keep -d generated http://www50.brinkster.com/vbfacileinpt/np.asmx?wsdl
Create PrimeClient.java which look like:
创建如下所示的 PrimeClient.java:
import javax.xml.ws.WebServiceRef;
import com.microsoft.webservices.*;
//the above namespace is from the generated code from the wsdl.
public class PrimeClient {
//Cant get this to work.. @WebServiceRef(wsdlLocation="http://www50.brinkster.com/vbfacileinpt/np.asmx?wsdl")
static PrimeNumbers service;
public static void main(String[] args) {
try {
service = new PrimeNumbers();
PrimeClient client = new PrimeClient();
client.doTest(args);
} catch(Exception e) {
e.printStackTrace();
}
}
public void doTest(String[] args) {
try {
System.out.println("Retrieving the port from the following service: " + service);
PrimeNumbersSoap pm = service.getPrimeNumbersSoap();
System.out.println("Invoking the getPrimeNumbersSoap operation ");
System.out.println(pm.getPrimeNumbers(100));
} catch(Exception e) {
e.printStackTrace();
}
}
}
Compile and run:
编译并运行:
C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\javac -cp generated PrimeClient.java
C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\java -cp .;generated PrimeClient
Retrieving the port from the following service: com.microsoft.webservices.PrimeN
umbers@19b5393
Invoking the getPrimeNumbersSoap operation
1,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97
回答by Tony
There are three ways to write a web service client
- Dynamic proxy
- Dynamic invocation interface (DII)
- Application client
- 动态代理
- 动态调用接口 (DII)
- 应用客户端
Example for Dynamic Proxy Client
动态代理客户端示例
import java.net.URL;
import javax.xml.rpc.Service;
import javax.xml.rpc.JAXRPCException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceFactory;
import dynamicproxy.HelloIF;
public class HelloClient {
public static void main(String[] args) {
try {
String UrlString = "Your WSDL URL"; //
String nameSpaceUri = "urn:Foo";
String serviceName = "MyHelloService";
String portName = "HelloIFPort";
System.out.println("UrlString = " + UrlString);
URL helloWsdlUrl = new URL(UrlString);
ServiceFactory serviceFactory =
ServiceFactory.newInstance();
Service helloService =
serviceFactory.createService(helloWsdlUrl,
new QName(nameSpaceUri, serviceName));
dynamicproxy.HelloIF myProxy =
(dynamicproxy.HelloIF)
helloService.getPort(
new QName(nameSpaceUri, portName),
dynamicproxy.HelloIF.class);
System.out.println(myProxy.sayHello("Buzz"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
回答by Thorbj?rn Ravn Andersen
The easiest I've found so far to use is the Idea IntelliJ wizard which - using Metro libraries - generate a very small code snippet which works fine with Java 6.
到目前为止,我发现最容易使用的是 Idea IntelliJ 向导,它使用 Metro 库生成一个非常小的代码片段,可以很好地与 Java 6 配合使用。