您的位置:首页 > 编程语言 > Java开发

CXF wsdl2Java

2011-07-12 09:38 176 查看
一. 简介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.html2.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 servicepublic 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内容如下:<?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"/> <arg value="http://localhost:9000/helloWorld?wsdl"/> <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如果显示其用法表示配置好了。输入: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.html2.5.调用web servicepublic 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: