<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    176142998

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      116 Posts :: 0 Stories :: 45 Comments :: 0 Trackbacks

    下載XFrie
    首先,去http://xfire.codehaus.org下載最新版本的XFire

    搭建webservice工程環境
    在eclipse里創建一個叫webservice的java工程,然后依次添加src-service、src-conf、src-test和src-util這幾個Source Folder以及web這個Folder
    目錄結構及文件如下:

    代碼
    1. webservice   
    2.      src-service   
    3.          cn.hidetoishandsome.xfire.model   
    4.              Book.java   
    5.          cn.hidetoishandsome.xfire.service   
    6.              BookService.java   
    7.          cn.hidetoishandsome.xfire.service.impl   
    8.              BookServiceImpl.java   
    9.      src-conf   
    10.          META-INF   
    11.              xfire   
    12.                  services.xml   
    13.      src-test   
    14.          cn.hidetoishandsome.xfire.test   
    15.              BookServiceTest.java   
    16.      src-util   
    17.          cn.hidetoishandsome.xfire.util   
    18.              XfireClientFactory.java   
    19.      web   
    20.          WEB-INF   
    21.              lib   
    22.              web.xml   
    23.          index.html   

    然后將解壓后的xfire的lib目錄下所有jar包和xfire-all-1.*.jar復制到WEB-INF/lib目錄
    web.xml內容如下:
    代碼
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"  
    3.          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
    4.   
    5.     <servlet>  
    6.         <servlet-name>xfire</servlet-name>  
    7.         <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>  
    8.     </servlet>  
    9.   
    10.     <servlet-mapping>  
    11.         <servlet-name>xfire</servlet-name>  
    12.         <url-pattern>/services/*</url-pattern>  
    13.     </servlet-mapping>  
    14.   
    15. </web-app>  

     

    寫一個BookService
    我們將創建一個從ISBM號得到Book的Title的簡單查詢Web服務
    首先創建Book.java

    代碼
    1. package cn.hidetoishandsome.xfire.model;   
    2.   
    3. public class Book {   
    4.   
    5.     private String title;   
    6.   
    7.     private String isbn;   
    8.   
    9.     public String getIsbn() {   
    10.         return isbn;   
    11.      }   
    12.   
    13.     public void setIsbn(String isbn) {   
    14.         this.isbn = isbn;   
    15.      }   
    16.   
    17.     public String getTitle() {   
    18.         return title;   
    19.      }   
    20.   
    21.     public void setTitle(String title) {   
    22.         this.title = title;   
    23.      }   
    24.   
    25. }   

    然后寫一個BookService接口BookService.java
    代碼
    1. package cn.hidetoishandsome.xfire.service;   
    2.   
    3. import cn.hidetoishandsome.xfire.model.Book;   
    4.   
    5. public interface BookService {   
    6.      Book findBookByISBN(String isbn);   
    7. }   

    然后是BookService的實現BookServiceImpl.java
    代碼
    1. package cn.hidetoishandsome.xfire.service.impl;   
    2.   
    3. import cn.hidetoishandsome.xfire.model.Book;   
    4. import cn.hidetoishandsome.xfire.service.BookService;   
    5.   
    6. public class BookServiceImpl implements BookService {   
    7.   
    8.     private Book book;   
    9.   
    10.     public BookServiceImpl() {   
    11.          book = new Book();   
    12.          book.setTitle("XFire Quick Start");   
    13.          book.setIsbn("123456");   
    14.      }   
    15.   
    16.     public Book findBookByISBN(String isbn) {   
    17.         if (isbn.equals(book.getIsbn()))   
    18.             return book;   
    19.         throw new RuntimeException("Can't find book");   
    20.      }   
    21.   
    22. }   

     

    在services.xml中配置要發布的服務
    在src-conf的META-INF/xfire目錄創建services.xml

    代碼
    1. <beans xmlns="http://xfire.codehaus.org/config/1.0">  
    2.     <service>  
    3.         <name>BookService</name>  
    4.         <namespace>http://localhost:8080/xfire/services/BookService</namespace>  
    5.         <serviceClass>cn.hidetoishandsome.xfire.service.BookService</serviceClass>  
    6.         <implementationClass>cn.hidetoishandsome.xfire.service.impl.BookServiceImpl</implementationClass>  
    7.     </service>  
    8. </beans>  

    其中name標簽決定了我們創建的該服務的WSDL的URL為http://xx.xx.xx/xx/xx/BookService?wsdl

     

    在Tomcat中發布
    可以簡單的修改Tomcat的server.xml來發布該Web服務,在<Host>標簽中添加以下內容:

    代碼
    1. Context path="/webservice" docBase="D:\project\webservice\web" reloadable="true"/>   

    現在打開瀏覽器訪問http://localhost:8080/webservice/services/BookService?wsdl來看看生成的WSDL文檔

     

    客戶端調用測試
    我們將使用一個XfireClientFactory.java工具類來幫我們調用該Web服務:

    代碼
    1. package cn.hidetoishandsome.xfire.util;   
    2.   
    3. import java.net.MalformedURLException;   
    4.   
    5. import org.apache.commons.logging.Log;   
    6. import org.apache.commons.logging.LogFactory;   
    7. import org.codehaus.xfire.client.XFireProxyFactory;   
    8. import org.codehaus.xfire.service.Service;   
    9. import org.codehaus.xfire.service.binding.ObjectServiceFactory;   
    10. import org.springframework.util.Assert;   
    11.   
    12. public class XfireClientFactory {   
    13.     private static XFireProxyFactory serviceFactory = new XFireProxyFactory();   
    14.   
    15.     private static final Log log = LogFactory.getLog(XfireClientFactory.class);   
    16.   
    17.     private XfireClientFactory() {   
    18.      }   
    19.   
    20.     public static <T> T getClient(String serviceURL, Class<T> serviceClass) {   
    21.          Assert.notNull(serviceURL);   
    22.          Assert.notNull(serviceClass);   
    23.          Service serviceModel = new ObjectServiceFactory().create(serviceClass);   
    24.         try {   
    25.             return (T) serviceFactory.create(serviceModel, serviceURL);   
    26.          } catch (MalformedURLException e) {   
    27.              log.error(e.getMessage(), e);   
    28.             return null;   
    29.          }   
    30.      }   
    31.   
    32. }   

    然后編寫一個BookServiceTest.java來調用我們剛才發布的Web服務:
    代碼
    1. package cn.hidetoishandsome.xfire.test;   
    2.   
    3. import cn.hidetoishandsome.xfire.service.BookService;   
    4. import cn.hidetoishandsome.xfire.util.XfireClientFactory;   
    5.   
    6. public class BookServieTest {   
    7.   
    8.     public static void main(String[] args) {   
    9.          String serviceURL = "http://localhost:8080/webservice/services/BookService";   
    10.         try {   
    11.              BookService service = XfireClientFactory.getClient(serviceURL, BookService.class);   
    12.              System.out.println("Book with ISBN '123456': 《" + service.findBookByISBN("123456").getTitle() + "》");   
    13.          } catch (Exception e) {   
    14.              e.printStackTrace();   
    15.          }   
    16.   
    17.      }   
    18. }   

    服務調用成功,Console打印內容如下:
    代碼
    1. Book with ISBN '123456': 《XFire Quick Start》   
    posted on 2008-07-25 18:53 飛飛 閱讀(327) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 一本色道久久88亚洲综合| 亚洲愉拍99热成人精品热久久 | 2022免费国产精品福利在线| 无码午夜成人1000部免费视频| 女人18毛片免费观看| 亚洲熟妇av一区二区三区| 精品人妻系列无码人妻免费视频| 人妻无码久久一区二区三区免费 | 亚洲国产精品13p| 亚洲欧洲日本精品| 丁香花在线观看免费观看图片 | 国产成人亚洲综合a∨| 精品无码国产污污污免费网站 | 亚洲色大成网站www永久网站 | 18pao国产成视频永久免费| 亚洲精品国产福利一二区| 久久久久精品国产亚洲AV无码| 亚洲日韩在线观看免费视频| 中文字幕在线亚洲精品 | 国产日产成人免费视频在线观看| 亚洲精品美女久久久久| 香蕉视频在线免费看| 亚洲午夜未满十八勿入| eeuss草民免费| 久久亚洲精品无码AV红樱桃| a毛片免费全部在线播放**| 亚洲国产成人爱av在线播放| 精品97国产免费人成视频| 97se亚洲综合在线| 免费无码成人AV在线播放不卡| 亚洲性一级理论片在线观看| 女人被弄到高潮的免费视频| 成人免费网站视频www| 又黄又爽一线毛片免费观看| 亚洲AV无码一区二区一二区| 最近免费中文字幕4| 亚洲天堂男人影院| 久久综合AV免费观看| 亚洲sss综合天堂久久久| 韩国欧洲一级毛片免费| 亚洲国产综合人成综合网站00|