標籤:cxf webservice
Apache CXF 是一個開放原始碼架構,提供了用於方便地構建和開發 Web 服務的可靠基礎架構。它允許建立高效能和可擴充的服務,您可以將這樣的服務部署在 Tomcat 和基於 Spring 的輕量級容器中,以及部署在更進階的伺服器上,例如 Jboss、IBM WebSphere 或 BEA WebLogic。
簡單介紹入門教程,也為自己記錄下。
(1)下載cxf包,http://cxf.apache.org/download.html,我這裡用的是2.4.0的包
匯入lib中的所有jar包,推薦使用library方式
(2)編寫webservice介面類,介面實作類別如下
介面需要指定annotation
@WebServicepublic 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{@Overridepublic String sayHi(String name) {System.out.println("hi,"+name);return "hi,"+name;}@Overridepublic 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方法,即可訪問所有方法
本文出自 “bulajunjun” 部落格,請務必保留此出處http://5148737.blog.51cto.com/5138737/1606249
cxf實現webservice