---
title: "Apache CXF"
author: "cynhafa"
date: "2013-10-31T19:37:00Z"
tags: []
slug: "apache-cxf"
draft: false
lastmod: "2014-02-11T14:34:25Z"
id: 2014534
url: "https://cynhafa.iteye.com/blog/2014534"
---

一. 简介

Apache CXF 是一个Service框架，他简化了Service的创建， CXF实现了JAX-WS2.0规范，并通过了JAX-WS2.0 TCK； CXF和Spring无缝集成；CXF支持多种传输协议（HTTP， JMS， Corba等）， 支持多种Binding数据格式（SOAP，XML，JSON等）， 支持多种DataBinding数据类型（JAXB， Aegis) 。CXF基于Interceptor的架构，使得整个框架非常易于扩展。

二. 如何发布并调用简单的web service实例

2.1．下载：apache-cxf-2.1.1 [http://cxf.apache.org/download.html]()

2.2． 新建java project ,并加入apache-cxf-2.0.7\\lib所有包，编写要发布的web service 接口和实现

import javax.jws.WebService;

@WebService

public interface HelloWorld {

public String sayHello(String text);

}

import javax.jws.WebService;

@WebService(endpointInterface="test.HelloWorld")

public class HelloWorldImpl implements HelloWorld {

public String sayHello(String text) {

return "Hello" + text ;

}

}

@WebService 注解表示是要发布的web 服务

name：用于Interface，属映射到wsdl:portType element的name属性。  
targetNamespace：用于Interface和implement，如果不指定，缺省会使用包名倒序做为wsdl名空间。

serviceName：用于implement，表示wsdl服务名。

portName：用于implement，表示wsdl:port 的name属性。

endpointInterface：用于implement，指定Interface全名，包括包名。

2.3.发布web service

public class Server {

protected Server() throws Exception {

System.out.println("Starting Server");

HelloWorldImpl implementor = new HelloWorldImpl();

String address = "[http://localhost:9000/helloWorld]() ";

Endpoint.publish(address, implementor);

}

public static void main(String args\[]) throws Exception {

new Server();

System.out.println("Server ready...");

Thread.sleep(5 * 60 * 1000);

System.out.println("Server exiting");

System.exit(0);

}

}

运行后，在浏览器中输入[http://localhost:9000/helloWorld?wsdl]() 将显示这个web service的wsdl.说明web service发布成功。

2.4.下面就开始创建一个客户端程序，访问这个web service, 同样新建java project ,并加入apache-cxf-2.0.7\\lib所有包，由于CXF已经提供wsdl转化成java 的命令工具，所以创建一个build.xml，用来生成客户端程序。Bulid.xml内容如下：

&lt;?xml version="1.0"?&gt;

&lt;project name="cxf wsdl2java" basedir="."&gt;

&lt;property name="cxf.home" location ="${basedir}/WebRoot/WEB-INF/"/&gt;

&lt;path id="cxf.classpath"&gt;

&lt;fileset dir="${cxf.home}/lib"&gt;

&lt;include name="\*.jar"/&gt;

&lt;/fileset&gt;

&lt;/path&gt;

&lt;target name="cxfWSDLToJava"&gt;

&lt;java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true"&gt;

&lt;arg value="-client"/&gt;

&lt;arg value="-d"/&gt;

&lt;arg value="src"/&gt;

&lt;arg value="[http://localhost:9000/helloWorld?wsdl"/](http://localhost:9000/helloWorld?wsdl%22%2F) &gt;

&lt;classpath&gt;

&lt;path refid="cxf.classpath"/&gt;

&lt;/classpath&gt;

&lt;/java&gt;

&lt;/target&gt;

&lt;/project&gt;

或者：配置环境变量%CXF\_HOME%=E:\\WebService\\CXF\\apache-cxf-2.1.1\\apache-cxf-2.1.1（以我的目录为例）,并在PATH后加上;%CXF\_HOME%\\bin

在cmd命令行中输入wsdl2java如果显示其用法表示配置好了。

输入：wsdl2java -d src - client [http://localhost:9000/helloWorld?wsdl]()

其作用上面的build.xml作用一样。

附加：wsdl2java用法：

wsdl2java -p com -d src -all aa.wsdl

-p 指定其wsdl的命名空间，也就是要生成代码的包名:

-d 指定要产生代码所在目录

-client 生成客户端测试web service的代码

-server 生成服务器启动web service的代码

-impl 生成web service的实现代码

-ant 生成build.xml文件

-all 生成所有开始端点代码：types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.

详细用法见：[http://cwiki.apache.org/CXF20DOC/wsdl-to-java.html]()

2.5.调用web service

public class MyClient {

public static void main(String\[] argv) {

HelloWorld hello = new HelloWorldImplService().getHelloWorldImplPort();

System.out.println(hello.sayHello("Tom") );

}

}

注意：运行时，要一定先要发布web sevice.

三. 参考资料

1.CXF 主页: [http://cxf.apache.org/]()

2\. CXF中文讨论组: [http://groups.google.com/group/cxf-zh]()

3\. Web service: [http://www.w3school.com.cn/webservices/index.asp]()  
4\. WSDL: [http://www.w3school.com.cn/wsdl/index.asp]()  
5\. SOAP:http://www.w3school.com.cn/soap/index.asp

本文来自CSDN博客，转载请标明出处：[http://blog.csdn.net/pengchua/archive/2008/07/30/2740565.aspx]()