VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • Java调用WebService方法总结(7)--CXF调用WebService

CXF = Celtix + XFire,继承了Celtix和XFire两大开源项目的精华,是一个开源的,全功能的,容易使用的WebService框架。文中所使用到的软件版本:Java 1.8.0_191、CXF 3.2.11。

1、准备

参考Java调用WebService方法总结(1)--准备工作

2、调用

2.1、Client方式

复制代码
public static void client(String param) {
    try {
        JaxWsDynamicClientFactory factroy = JaxWsDynamicClientFactory.newInstance();
        //DynamicClientFactory factroy = DynamicClientFactory.newInstance();
        Client client = factroy.createClient("http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl");
        Object[] results = client.invoke("toTraditionalChinese", param);
        System.out.println("client方式:" + results[0]);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
复制代码

该方式需要用到如下jar文件,少jar文件会报各种各样的错误。

cxf-core-3.2.11.jar、cxf-rt-bindings-soap-3.2.11.jar、cxf-rt-databinding-jaxb-3.2.11.jar、cxf-rt-frontend-jaxws-3.2.11.jar、cxf-rt-frontend-simple-3.2.11.jar、cxf-rt-transports-http-3.2.11.jar、cxf-rt-wsdl-3.2.11.jar、stax2-api-3.1.4.jar、woodstox-core-5.0.3.jar、xmlschema-core-2.2.4.jar、wsdl4j-1.6.3.jar。这些jar文件在下载的CXF包里都有。

2.2、wsdl2java方式

2.2.1、根据wsdl生成代码

执行命令:

%CXF_HOME%/bin/wsdl2java.bat -p com.inspur.ws.cxf -client http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl

-p 指定包名  -client 生成客户端调用样例

更多的参数说明详见官方文档:http://cxf.apache.org/docs/wsdl-to-java.html

2.2.1、用生成代码的调用WebService

复制代码
public static void wsdl2java(String param) {
    com.inspur.ws.cxf.TraditionalSimplifiedWebService service = new com.inspur.ws.cxf.TraditionalSimplifiedWebService();
    com.inspur.ws.cxf.TraditionalSimplifiedWebServiceSoap port = service.getTraditionalSimplifiedWebServiceSoap();
    String reuslt = port.toTraditionalChinese(param);
    System.out.println("wsdl2java方式:" + reuslt);
    
    com.inspur.ws.cxf.TraditionalSimplifiedWebServiceSoap port2 = service.getTraditionalSimplifiedWebServiceSoap12();
    reuslt = port2.toTraditionalChinese(param);
    System.out.println("wsdl2java方式2:" + reuslt);
}
复制代码

2.3、Simple Frontend Client Proxy方式

这种方式需要服务端是CXF用Simple Frontend发布的WebService。这里用官网的demo来演示。

2.3.1、创建服务端

a、接口

package com.inspur.ws.cxf.server;

public interface HelloWorld {
    String sayHi(String text);
}

b、实现类

复制代码
package com.inspur.ws.cxf.server;

public class HelloWorldImpl implements HelloWorld {
    public String sayHi(String text) {
        return "Hello " + text;
    }
}
复制代码

c、发布服务

复制代码
package com.inspur.ws.cxf.server;

import org.apache.cxf.frontend.ServerFactoryBean;

public final class Server {
    private Server() {
    }

    public static void startServer() throws Exception {
        HelloWorldImpl helloworldImpl = new HelloWorldImpl();
        ServerFactoryBean svrFactory = new ServerFactoryBean();
        svrFactory.setServiceClass(HelloWorld.class);
        svrFactory.setAddress("http://localhost:9000/Hello");
        svrFactory.setServiceBean(helloworldImpl);
        svrFactory.create();
    }

    public static void main(String args[]) throws Exception {
        startServer();
        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);
        System.out.println("Server exiting");
        System.exit(0);
    }
}
复制代码

发布服务时会用到好多jar文件,如果少jar文件会报各种奇怪的错误;可以把%CXF_HOME%/lib下的jar文件都拿过来,通过不断删除一些jar文件然后重启发布服务看有没有问题的方法来删除多余的jar文件。

2.3.2、客户端

复制代码
public static void clientProxyFactoryBean (String param) {
    ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
    factory.setServiceClass(HelloWorld.class);
    factory.setAddress("http://localhost:9000/Hello");
    HelloWorld serice = (HelloWorld) factory.create();
    System.out.println(serice.sayHi(param));
}
复制代码

2.4、调用完整代码

复制代码
package com.inspur.ws;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import com.inspur.ws.cxf.server.HelloWorld;

/**
 * CXF调用WebService
 *
 */
public class CXF {
    /**
     * Client方式
     * @param param
     */
    public static void client(String param) {
        try {
            JaxWsDynamicClientFactory factroy = JaxWsDynamicClientFactory.newInstance();
            //DynamicClientFactory factroy = DynamicClientFactory.newInstance();
            Client client = factroy.createClient("http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl");
            Object[] results = client.invoke("toTraditionalChinese", param);
            System.out.println("client方式:" + results[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * wsdl2java方式
     * @param param
     */
    public static void wsdl2java(String param) {
        com.inspur.ws.cxf.TraditionalSimplifiedWebService service = new com.inspur.ws.cxf.TraditionalSimplifiedWebService();
        com.inspur.ws.cxf.TraditionalSimplifiedWebServiceSoap port = service.getTraditionalSimplifiedWebServiceSoap();
        String reuslt = port.toTraditionalChinese(param);
        System.out.println("wsdl2java方式:" + reuslt);
        
        com.inspur.ws.cxf.TraditionalSimplifiedWebServiceSoap port2 = service.getTraditionalSimplifiedWebServiceSoap12();
        reuslt = port2.toTraditionalChinese(param);
        System.out.println("wsdl2java方式2:" + reuslt);
    }
    
    /**
     * Simple Frontend Client Proxy方式
     * @param param
     */
    public static void clientProxyFactoryBean (String param) {
        ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
        factory.setServiceClass(HelloWorld.class);
        factory.setAddress("http://localhost:9000/Hello");
        HelloWorld serice = (HelloWorld) factory.create();
        System.out.println("Simple Frontend Client Proxy方式:" + serice.sayHi(param));
    }
    
    public static void main(String[] args) {
        client("小学");
        wsdl2java("大学");
        clientProxyFactoryBean("小学");
    }
}
复制代码

 

来源:https://www.cnblogs.com/wuyongyin/p/11884197.html


相关教程