CXF webservice完整例子

來源:互聯網
上載者:User

標籤:查看   點擊   system   技術分享   new   play   path   nts   images   

Web Service技術應用廣泛,可實現不同平台的資料交換,現做了一個CXF webservice小例子,供webservice初學者參考.

1.環境搭建

1.1  下載 Apache CXF  可以去官方下載 http://cxf.apache.org/ 。也可點擊http://pan.baidu.com/s/1jIPyOYU 我分享的雲端硬碟下載(apache-cxf-2.4.2).  解壓檔案

1.2  配置Apache CXF環境變數: 建立一個CXF_HOEM變數,值為CXF架構所在根目錄

  

   在path中添加CXF_HOEM變數

  

 

1.3  建立java項目   引入 D:\work\apache-cxf-2.4.2\lib 目錄下的jar包

2.代碼編寫

2.1  建立IHelloService.java 介面

package com.service;import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic interface IHelloService {    public String sayHello(@WebParam(name = "text") String text);    public User getUser(String id);}
View Code

 

2.2  建立HelloServiceImpl.java實現 IHelloService 介面

package com.service;import java.util.Date;import javax.jws.WebService;@WebService(endpointInterface = "com.service.IHelloService", serviceName = "HelloWorld")public class HelloServiceImpl implements IHelloService {    @Override    public String sayHello(String text) {        return "Hello x " + text;    }    @Override    public User getUser(String id) {        User u = new User();        u.setId(id);        u.setAge(10);        u.setName("小明");        u.setBirthday(new Date(new Date().getTime() - 10 * 12 * 30 * 24 * 3600));        return u;    }}
View Code

 

2.3  編寫 webServiceApp.java類來暴露 web服務

package com.service;import javax.xml.ws.Endpoint;public class WebServiceApp {    public static void main(String[] args) {        System.out.println("web service start");        HelloServiceImpl implementor = new HelloServiceImpl();        String address = "http://localhost:8080/HelloService";        Endpoint.publish(address, implementor);        System.out.println("web service started");    }}
View Code

 

2.4  run webServiceApp.java 類來啟動服務。 訪問 http://localhost:8080/HelloService?wsdl 查看是否成功

   

 

2.5 編寫用戶端訪問服務
======== 方法1:

建立用戶端的代理,產生factory對象,用於調用服務端的方法

package com.service;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class HelloServiceClient {    public static void main(String[] args) {        JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();        svr.setServiceClass(IHelloService.class);        svr.setAddress("http://localhost:8080/HelloService");        IHelloService hw = (IHelloService) svr.create();        System.out.println(hw.sayHello("你好"));        User u = hw.getUser("1");        System.out.println(u.getName());    }}

 

======== 方法2:
通過命令自動產生用戶端檔案
 1.   cmd 進入doc視窗, 進入apache-cxf-2.4.2\bin路徑
 2.   運行 wsdl2java -frontend jaxws21 -p com.client -d D:\ -client -autoNameResolution http://localhost:8080/HelloWorld?wsdl    會在D盤產生 com\client檔案夾,裡面有組建編號的用戶端檔案。將檔案拷貝到項目中

  

3.  調用介面

 

package com.main;import com.client.HelloWorld;import com.client.IHelloService;public class ClientMain {    public static void main(String[] args) {        HelloWorld factory = new HelloWorld();        IHelloService hw = factory.getHelloServiceImplPort();        System.out.println(hw.sayHello("sss"));        System.out.println(hw.getUser("11").getName());    }}

 

下面連結可下載上述源碼

http://pan.baidu.com/s/1kUZVZHt  希望能幫到你

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.