ITeye博客

CXF WSDL2JAVA

solomon · 2010-12-24 10:57 · others · 非技术 · Markdown

转自 : http://blog.chinaunix.net/u2/73798/showart_2002108.html

将wdl binder http://service.test.com/替换为自己的namespace。

命令格式如下:

wsdl2java -b jaxb-binding-date.xml http://localhost:8080/cxfservice/services/XXXXX?wsdl

#########################################################

CXF wsdl2Java

![](/admin/templates/default/images/right_line.gif" border="0" alt="" width="502" height="9)

一. 简介 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内容如下:

或者:配置环境变量%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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。