1、自动生成客户端代码:

先把CXF下到本地,本例中我下的是apache-cxf-3.1.2,然后在命令行里到相应路径执行命令:

D:\soft\DevelopSoft\apache-cxf-3.1.2\bin>wsdl2java -d D:\\src -client http://172.16.10.87/platform3.0/webService/TestWebservice?wsdl

D:\soft\DevelopSoft\apache-cxf-3.1.2\bin>wsdl2java -encoding utf-8 -d D:\\src -client http://localhost:8080/dbws?wsdl


 D:\soft\DevelopSoft\apache-cxf-3.1.2\bin>wsdl2java -encoding utf-8 -d D:\\src -client http://localhost:8080/dbws?wsdl


如此wsdl2java工具会自动为你生成客户端可调用代码在D盘的src目录下。


2、在程序中使用

本例中服务端有一个接口​ITestWebservice​以及一个实现类​TestWebserviceImpl。

自动生成的代码里会有一个​ITestWebservice​以及一个​TestWebserviceImplService​,与上边两个算是一个对应关系。

使用的时候有两种方式:

一、可以只通过​ITestWebservice​加地址的方式来调用

二、也可以通过​ITestWebservice​加​TestWebserviceImplService​的方式来调用

下边的代码里包含了这两种方式:

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.sdyy.webservice.ITestWebservice;
import com.sdyy.webservice.TestWebserviceImplService;

/**
* @ClassName: WebServiceTest
* @Description: TODO
* @author: liuyx
* @date: 2015年9月27日下午5:22:15
*/
public class WebServiceTest3 {
private static final String testUrl = "http://172.16.10.87/platform3.0/webService/TestWebservice?wsdl";
public static void main1(String[] args) throws Exception {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.getInInterceptors().add(new LoggingInInterceptor());
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
factoryBean.setServiceClass(ITestWebservice.class);
factoryBean.setAddress(testUrl);
ITestWebservice impl = (ITestWebservice) factoryBean.create();
System.out.println(impl.testOut("咯咯咯"));
}
public static void main(String[] args) throws Exception {
TestWebserviceImplService factory = new TestWebserviceImplService();
ITestWebservice testOut = factory.getTestWebserviceImplPort();
System.out.println(testOut.testOut("GEGEGE"));
}
}




动态创建客户端的方式,请参阅本博另一篇。​