什么是CXF?
Apache CXF=Celtix+XFire
jax-ws是Sun公司发布的一套开发WebService服务的标准,CXF是在新标准ax-ws下开发出来的WebService.
支持多种协议:
SOAP
XML/HTTP
CORBA(Commom Object Request Broker Architecture公共对象请求代理体系结构,早期语言使用的WS)
可以与Spring进行快速无缝的整合
灵活的部署:可以运行有Tomcat,Jboss,Jetty(内置),IBMWS,BeaWs上面
CXF的安装和配置
1、我的版本是apache-cxf-2.4.0,下载完成后将其解压到某个目录下【注:目录名字尽量不要有中文】
2、安装CXF的其他支持项目:ant 和 Tomcat
3、安装完成后设置环境变量:
CXF_HOME: D:\Ncode\cxf\apache-cxf-2.4.0\apache-cxf-2.4.0\samples\java_first_pojo
ANT_HOME: D:\Ncode\cxf\apache-cxf-2.4.0\apache-cxf-2.4.0\samples\java_first_pojo
Path = D:\Ncode\cxf\apache-cxf-2.4.0\apache-cxf-2.4.0\samples\java_first_pojo; D:\Ncode\cxf\apache-cxf-2.4.0\apache-cxf-2.4.0\samples\java_first_pojo
CLASSPATH=.;%CXF_HOME%\lib\cxf-manifest.jar;.\build\classes
在dos命令中使用命令测试是否成功
C:\Documents and Settings\Administrator>ant
Buildfile: build.xml does not exist!
Build failed
C:\Documents and Settings\Administrator>wsdl2java
Missing argument: wsdlurl
如果你显示的和上面一样,说明你已经配置成功啦
用ant来构造第一个CXF示例:
切换到实例目录:
C:\Documents and Settings\Administrator>d:
D:\>cd D:\Ncode\cxf\apache-cxf-2.4.0\samples\java_first_pojo //切换目录
D:\Ncode\cxf\apache-cxf-2.4.0\samples\java_first_pojo>ant //执行ant操作
Buildfile: D:\Ncode\cxf\apache-cxf-2.4.0\samples\java_first_pojo\build.xml
[mkdir] Created dir: D:\Ncode\cxf\apache-cxf-2.4.0\samples\java_first_pojo\build
[loadfile] Do not set property srcbuild.classpath as its length is 0.
BUILD FAILED
D:\Ncode\cxf\apache-cxf-2.4.0\samples\java_first_pojo\build.xml:22: The following error occurred while executing this line:
D:\Ncode\cxf\apache-cxf-2.4.0\samples\common_build.xml:75: Apache CXF requires Java version 1.5 or higher. You are currently using Java version 1.7.
Total time: 0 seconds
D:\Ncode\cxf\apache-cxf-2.4.0\samples\java_first_pojo>
启动服务:
D:\Ncode\cxf\apache-cxf-2.4.0\samples\java_first_pojo>ant server
Buildfile: D:\Ncode\cxf\apache-cxf-2.4.0\samples\java_first_pojo\build.xml
[loadfile] Do not set property srcbuild.classpath as its length is 0.
maybe.generate.code:
compile:
[mkdir] Created dir: D:\Ncode\cxf\apache-cxf-2.4.0\samples\java_first_pojo\build\classes
[mkdir] Created dir: D:\Ncode\cxf\apache-cxf-2.4.0\samples\java_first_pojo\build\src
[javac] D:\Ncode\cxf\apache-cxf-2.4.0\samples\common_build.xml:127: warning:'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 4 source files to D:\Ncode\cxf\apache-cxf-2.4.0\samples\java_first_pojo\build\classes
[loadfile] Do not set property srcbuild.classpath as its length is 0.
copy.maven.resources:
build:
server:
[java] Server ready...
[java] sayHi called
打开新的界面执行客户端操作:
D:\Ncode\cxf\apache-cxf-2.4.0\samples\java_first_pojo>ant client
Buildfile: D:\Ncode\cxf\apache-cxf-2.4.0\samples\java_first_pojo\build.xml
[loadfile] Do not set property srcbuild.classpath as its length is 0.
maybe.generate.code:
compile:
[javac] D:\Ncode\cxf\apache-cxf-2.4.0\samples\common_build.xml:127: warning:
'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to
false for repeatable builds
[loadfile] Do not set property srcbuild.classpath as its length is 0.
copy.maven.resources:
build:
client:
[java] Invoke sayHi()....
[java] Hello redarmychen
BUILD SUCCESSFUL
Total time: 3 seconds
用ant将它发布到Tomcat服务器上:
1、 执行ant clean方法清除以前生成的代码
2、 执行ant war打包此应用
3、 执行ant deplay-Dtomcat=true将项目发布到tomcat的服务器上
4、 启动tomcat服务器
5、 打开IE的地址输入栏输入:http://localhost:9999/helloworld/services/hello_world?wsdl
或:http://localhost:9999/helloworld/services
6、 在命令行中执行:
ant client-servlet –Dbase.url=http://localhost:9999
或在MyEclipse的WebServiceExplorer中访问这个WebService
7、 停止tomcat服务器
8、 卸载应用:ant undeploy –Dtomcat=true
9、 清空项目:ant clean
注:ant war,在build中会有一个war文件,把这个文件放到tomcat/webapps下,直接打开tomcat就启动成功了