cxf實現webservice

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.