Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它容许建立高性能和可扩展的服务,您能够将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM WebSphere 或 BEA WebLogic。html
简单介绍入门教程,也为本身记录下。java
(1)下载cxf包,http://cxf.apache.org/download.html,我这里用的是2.4.0的包web
网盘地址以下:http://yun.baidu.com/share/link?shareid=564842495&uk=2836507213apache
导入lib中的全部jar包,推荐使用library方式服务器
(2)编写webservice接口类,接口实现类以下架构
接口须要指定annotation框架
@WebService public interface IHello { public String sayHi(String name); public String printName(String name); }
编写上述接口的实现类,annotation指定了endpointInterface与serviceNameide
@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)编写服务端,并启动性能
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来发布该服务,固然也能够使用JaxWsServerFactoryBeanspa
运行main方法,访问http://localhost/cxf/hello?wsdl 能够看到该服务的wsdl文件
(4)编写客户端
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方法,便可访问全部方法