本文共 1743 字,大约阅读时间需要 5 分钟。
Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM WebSphere 或 BEA WebLogic。
简单介绍入门教程,也为自己记录下。
(1)下载cxf包,
网盘地址如下:http://yun.baidu.com/share/link?shareid=564842495&uk=2836507213
导入lib中的所有jar包,推荐使用library方式
(2)编写webservice接口类,接口实现类如下
接口需要指定annotation
1 2 3 4 5 6 | @WebService public interface IHello { public String sayHi(String name); public String printName(String name); } |
编写上述接口的实现类,annotation指定了endpointInterface与serviceName
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @WebService (endpointInterface= "com.xj.service.IHello" ,serviceName= "hello1Service" ) public class HelloImpl implements IHello{ @Override public String sayHi(String name) { System.out.println( "hi," +name); return "hi," +name; } @Override public String printName(String name) { System.out.println( "my name is," +name); return "my name is," +name; } } |
(3)编写服务端,并启动
1 2 3 4 5 6 7 8 9 | public class RunServer { public static void main(String[] args) { IHello hello = new HelloImpl(); Endpoint.publish( "http://localhost/cxf/hello" , hello); System.out.println( "启动server端" ); } } |
此处同样采用的是endpoint来发布该服务,当然也可以使用JaxWsServerFactoryBean
运行main方法,访问 可以看到该服务的wsdl文件
(4)编写客户端
1 2 3 4 5 6 7 8 9 10 11 12 | public class RunClient { public static void main(String[] args) { JaxWsProxyFactoryBean proxy = new JaxWsProxyFactoryBean(); proxy.setServiceClass(IHello. class ); proxy.setAddress( "http://localhost/cxf/hello?wsdl" ); IHello hello = (IHello)proxy.create(); System.out.println(hello.sayHi( "xiejun" )); System.out.println(hello.printName( "xiexie" )); } } |
使用JaxWsProxyFactoryBean创建代理,指定service类,指定wsdl地址,
调用代理类的create方法,即可访问所有方法
本文转自布拉君君 51CTO博客,原文链接:http://blog.51cto.com/5148737/1606249,如需转载请自行联系原作者