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

查看:21
本文介绍了简单地使用 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 库.使用它,您将有多种调用网络服务的选项:

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天全站免登陆