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.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();
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);
}
}
2.4.下面就开始创建一个客户端程序,访问这个web service, 同样新建java project ,并加入apache-cxf-2.0.7\lib所有包,由于CXF已经提供wsdl转化成java 的命令工具,所以创建一个build.xml,用来生成客户端程序。Bulid.xml内容如下:
<?xml version="1.0"?>
<project name="cxf wsdl2java" basedir=".">
<property name="cxf.home" location ="${basedir}/WebRoot/WEB-INF/"/>
<path id="cxf.classpath">
<fileset dir="${cxf.home}/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="cxfWSDLToJava">
<java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true">
<arg value="-client"/>
<arg value="-d"/>
<arg value="src"/>
<classpath>
<path refid="cxf.classpath"/>
</classpath>
</java>
</target>
</project>
或者:配置环境变量%CXF_HOME%=E:\WebService\CXF\apache-cxf-2.1.1\apache-cxf-2.1.1(以我的目录为例),并在PATH后加上;%CXF_HOME%\bin
在cmd命令行中输入wsdl2java如果显示其用法表示配置好了。
其作用上面的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.
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.
相关推荐
2.用cxf开发webservice 3.这个服务端和客户端的小demo 在服务端 对外开放接口服务,然后在客户端 调用服务端的方法, 实现客户端(一个javaweb项目)对服务端(javaweb项目)方法的调用, 实际上就是发送和接收消息...
cxf 开发webservice客户端 代码经过测试
CXF开发webservice服务端,希望对大家有所帮助
最近用CXF开发webservice,发现老是报jar包的错误,但是又不能把所有从官网下载下来的都复制过来。官网下载的有好几十个呢,这是官网CXF的下载地址:http://cxf.apache.org/download.html。于是网上整理了一下,做个...
cxf 开发webservice与调用demo
cxf开发webservice.docxcxf开发webservice.docx
傻瓜试记录开发步骤,具体实现自己去完成。 如果看不懂,找自已原因。
cxf开发webService.pdf
第一天: 什么是webservice? 从案例(便民查询网站)分析如何实现?...CXF开发webservice: CXF入门程序 Spring+cxf整合(重点) CXF发布rest的webservice。(重点) 综合案例: 实现便民查询网站
CXF开发webservice客户端,详细步骤 希望对大家有所帮助
通过实例讲解运用spring和CXF开发webservice,通过调用webservice中的方法向数据库中插入数据
本java工程实现了spring+cxf快速开发webservice jar包齐全导入myeclipse可直接二次开发
03.使用CXF开发WebService客户端
通过CXF开发webService接口(soap协议、maven项目).pdf通过CXF开发webService接口(soap协议、maven项目).pdf
cxf开发webservice与spring整合所需jar包
开发环境jdk1.7,cxf版本3.1.17。 spring整合CXF开发webService所需jar包。
02.CXF功能概述_CXF发展历史和使用CXF开发WebService服务器端
cxf开发webservice客户端和服务器端,详细的cxf使用指令,有用的示例代码及注释。
Spring+CXF开发WebService源代码,包括所需要的基本jar,调试通过,适合初学者入门学习。
这是讲解cxf的权威开发文档 文档内容:Apache CXF 提供方便的Spring整合方法,可以通过注解、Spring标签式配置来暴露Web Services和消费Web Services