简单地使用Java中的Web服务 [英] Simply consuming a web service in Java

查看:158
本文介绍了简单地使用Java中的Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的SOAP Web服务,我需要从Java客户端使用它。在不使用任何第三方库的情况下,最简单的方法是什么?要求是在每次调用ws之前从web.xml读取主机和端口。

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.

推荐答案

我可以推荐你CXF库。使用它,您将有几个调用Web服务的选项:

I can recommend you CXF library. Using it you will have several options for calling web services:


  1. 使用动态代理用于调用(不需要使用wsdl2java创建Java存根)。

  1. Use dynamic proxy for 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);


  • 使用从WSDL生成的Java存根,使用wsdl2java。

  • Using Java stub generated from WSDL, using wsdl2java.

    如果你的服务器是使用CXF创建的,你可以直接重用你的接口代码(而不是在你的界面创建的WSDL上使用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!)

    对于#2和#3,以下代码举例说明了CXF的用法:

    For both #2 and #3, the following code exemplifies the CXF usage:

    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.setAddress("http://admin:password@localhost:8080/services/MyService");
    factory.setServiceClass(ITest.class);
    ITest client = (ITest) factory.create();
    client.test();
    

    这篇关于简单地使用Java中的Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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