CXF = Celtix + XFire,继承了Celtix和XFire两大开源项目的精华,是一个开源的,全功能的,容易使用的WebService框架。文中所使用到的软件版本:Java 1.8.0_191、CXF 3.2.11。
1、准备
2、调用
2.1、Client方式
public static voidclient(String param) {try{
JaxWsDynamicClientFactory factroy=JaxWsDynamicClientFactory.newInstance();//DynamicClientFactory factroy = DynamicClientFactory.newInstance();
Client client = factroy.createClient("/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 /WebServices/TraditionalSimplifiedWebService.asmx?wsdl
-p 指定包名 -client 生成客户端调用样例
更多的参数说明详见官方文档:/docs/wsdl-to-java.html
2.2.1、用生成代码的调用WebService
public static voidwsdl2java(String param) {
com.inspur.ws.cxf.TraditionalSimplifiedWebService service= newcom.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、接口
packagecom.inspur.ws.cxf.server;public interfaceHelloWorld {
String sayHi(String text);
}
b、实现类
packagecom.inspur.ws.cxf.server;public class HelloWorldImpl implementsHelloWorld {publicString sayHi(String text) {return "Hello " +text;
}
}
c、发布服务
packagecom.inspur.ws.cxf.server;importorg.apache.cxf.frontend.ServerFactoryBean;public final classServer {privateServer() {
}public static void startServer() throwsException {
HelloWorldImpl helloworldImpl= newHelloWorldImpl();
ServerFactoryBean svrFactory= newServerFactoryBean();
svrFactory.setServiceClass(HelloWorld.class);
svrFactory.setAddress("http://localhost:9000/Hello");
svrFactory.setServiceBean(helloworldImpl);
svrFactory.create();
}public static void main(String args[]) throwsException {
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 voidclientProxyFactoryBean (String param) {
ClientProxyFactoryBean factory= newClientProxyFactoryBean();
factory.setServiceClass(HelloWorld.class);
factory.setAddress("http://localhost:9000/Hello");
HelloWorld serice=(HelloWorld) factory.create();
System.out.println(serice.sayHi(param));
}
2.4、调用完整代码
packagecom.inspur.ws;importorg.apache.cxf.endpoint.Client;importorg.apache.cxf.frontend.ClientProxyFactoryBean;importorg.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;importcom.inspur.ws.cxf.server.HelloWorld;/*** CXF调用WebService
**/
public classCXF {/*** Client方式
*@paramparam*/
public static voidclient(String param) {try{
JaxWsDynamicClientFactory factroy=JaxWsDynamicClientFactory.newInstance();//DynamicClientFactory factroy = DynamicClientFactory.newInstance();
Client client = factroy.createClient("/WebServices/TraditionalSimplifiedWebService.asmx?wsdl");
Object[] results= client.invoke("toTraditionalChinese", param);
System.out.println("client方式:" + results[0]);
}catch(Exception e) {
e.printStackTrace();
}
}/*** wsdl2java方式
*@paramparam*/
public static voidwsdl2java(String param) {
com.inspur.ws.cxf.TraditionalSimplifiedWebService service= newcom.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方式
*@paramparam*/
public static voidclientProxyFactoryBean (String param) {
ClientProxyFactoryBean factory= newClientProxyFactoryBean();
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 voidmain(String[] args) {
client("小学");
wsdl2java("大学");
clientProxyFactoryBean("小学");
}
}
View Code