cxf实现webservice
? Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM WebSphere 或 BEA WebLogic。 简单介绍入门教程,也为自己记录下。 (1)下载cxf包,http://cxf.apache.org/download.html,我这里用的是2.4.0的包 ? ? ? ? 网盘地址如下:http://yun.baidu.com/share/link?shareid=564842495&uk=2836507213 ? ? ? ? 导入lib中的所有jar包,推荐使用library方式 (2)编写webservice接口类,接口实现类如下 接口需要指定annotation @WebService public?interface?IHello?{ public?String?sayHi(String?name); ???? public?String?printName(String?name); } 编写上述接口的实现类,annotation指定了endpointInterface与serviceName @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来发布该服务,当然也可以使用JaxWsServerFactoryBean 运行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方法,即可访问所有方法 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |