简单地使用 Java 中的 Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/291847/
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
Simply consuming a web service in Java
提问by Deano
I have a very simple SOAP web service that I need to consume from a Java client. What is the easiest way to accomplish this without using any third party libraries? A requirement is that the host and port is read from the web.xml before every call to the ws.
我有一个非常简单的 SOAP Web 服务,我需要从 Java 客户端使用它。在不使用任何第三方库的情况下完成此任务的最简单方法是什么?一个要求是在每次调用 ws 之前从 web.xml 中读取主机和端口。
回答by Chris Vest
Without using any third party libraries? Get to know the SOAP standard really well and learn to love SAX.
不使用任何第三方库?真正深入地了解 SOAP 标准并学会爱上 SAX。
If you can't love SAX, then lax your no-third-party-libs requirement and use StAX (with woodstox) instead.
如果您不喜欢 SAX,那么请放宽您的无第三方库要求并改用 StAX(使用 woodstox)。
This approach might be the "easiest" (considering the no-third-party-libs requirement) but I don't think it will be easy.
这种方法可能是“最简单的”(考虑到无第三方库的要求),但我认为这并不容易。
回答by Dónal
If you can relax your "no 3rd party libraries" requirement, and you have a WSDL for the web service then Axis makes it really easy. Just compile the WSDL using wsdl2java, and you can use the generated Java classes to consume the web service.
如果您可以放宽您的“无第 3 方库”要求,并且您有一个用于 Web 服务的 WSDL,那么 Axis 会让它变得非常简单。只需使用 wsdl2java 编译 WSDL,您就可以使用生成的 Java 类来使用 Web 服务。
回答by anjanb
Depending on which version of JAVA you're using, some of the JAX-WS is built into it. JDK 6 has Java's JAX-WS standard implementation and you could just use it.
根据您使用的 JAVA 版本,其中内置了一些 JAX-WS。JDK 6 具有 Java 的 JAX-WS 标准实现,您可以直接使用它。
See the following:
请参阅以下内容:
JAX-WS 2.1 and JAXB 2.1 is available in JDK 6 Update 4 release
Getting Started with JAX-WS Web Services(tutorial to use the JDK built-in JAX-WS for deploying and consuming a web service)
JAX-WS Web 服务入门(使用 JDK 内置 JAX-WS 部署和使用Web 服务的教程)
回答by FoxyBOA
I can recommend you CXF library. Using it you will have several options for calling web services:
我可以向您推荐 CXF 库。使用它,您将有多种调用 Web 服务的选项:
Use dynamic proxyfor calling (don't need to make Java stubs using wsdl2java).
DynamicClientFactory dcf = DynamicClientFactory.newInstance(); Client client = dcf.createClient("http://admin:password@localhost:8080"+ "/services/MyService?wsdl"); Object[] a = client.invoke("test", ""); System.out.println(a);
Using Java stub generated from WSDL, using wsdl2java.
If your server was created using CXF you can reuse your interface code directly (instead of using wsdl2java on the WSDL which was created from your interface!)
使用动态代理进行调用(不需要使用 wsdl2java 制作 Java 存根)。
DynamicClientFactory dcf = DynamicClientFactory.newInstance(); Client client = dcf.createClient("http://admin:password@localhost:8080"+ "/services/MyService?wsdl"); Object[] a = client.invoke("test", ""); System.out.println(a);
使用从 WSDL 生成的 Java 存根,使用 wsdl2java。
如果您的服务器是使用 CXF 创建的,您可以直接重用您的接口代码(而不是在从您的接口创建的 WSDL 上使用 wsdl2java!)
For both #2 and #3, the following code exemplifies the CXF usage:
对于 #2 和 #3,以下代码举例说明了 CXF 用法:
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://admin:password@localhost:8080/services/MyService");
factory.setServiceClass(ITest.class);
ITest client = (ITest) factory.create();
client.test();