176142998

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            116 Posts :: 0 Stories :: 45 Comments :: 0 Trackbacks

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

          搭建webservice工程環(huán)境
          在eclipse里創(chuàng)建一個(gè)叫webservice的java工程,然后依次添加src-service、src-conf、src-test和src-util這幾個(gè)Source Folder以及web這個(gè)Folder
          目錄結(jié)構(gòu)及文件如下:

          代碼
          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復(fù)制到WEB-INF/lib目錄
          web.xml內(nèi)容如下:
          代碼
          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>  

           

          寫(xiě)一個(gè)BookService
          我們將創(chuàng)建一個(gè)從ISBM號(hào)得到Book的Title的簡(jiǎn)單查詢Web服務(wù)
          首先創(chuàng)建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. }   

          然后寫(xiě)一個(gè)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的實(shí)現(xiàn)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中配置要發(fā)布的服務(wù)
          在src-conf的META-INF/xfire目錄創(chuàng)建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標(biāo)簽決定了我們創(chuàng)建的該服務(wù)的WSDL的URL為http://xx.xx.xx/xx/xx/BookService?wsdl

           

          在Tomcat中發(fā)布
          可以簡(jiǎn)單的修改Tomcat的server.xml來(lái)發(fā)布該Web服務(wù),在<Host>標(biāo)簽中添加以下內(nèi)容:

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

          現(xiàn)在打開(kāi)瀏覽器訪問(wèn)http://localhost:8080/webservice/services/BookService?wsdl來(lái)看看生成的WSDL文檔

           

          客戶端調(diào)用測(cè)試
          我們將使用一個(gè)XfireClientFactory.java工具類(lèi)來(lái)幫我們調(diào)用該Web服務(wù):

          代碼
          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. }   

          然后編寫(xiě)一個(gè)BookServiceTest.java來(lái)調(diào)用我們剛才發(fā)布的Web服務(wù):
          代碼
          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. }   

          服務(wù)調(diào)用成功,Console打印內(nèi)容如下:
          代碼
          1. Book with ISBN '123456': 《XFire Quick Start》   
          posted on 2008-07-25 18:53 飛飛 閱讀(329) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 海兴县| 鸡西市| 德阳市| 渭南市| 沙坪坝区| 新民市| 新野县| 西充县| 开远市| 望谟县| 长沙县| 平塘县| 清丰县| 长宁县| 讷河市| 镇江市| 布尔津县| 吉木萨尔县| 故城县| 会泽县| 冀州市| 全州县| 正镶白旗| 方正县| 惠来县| 科尔| 博爱县| 澄江县| 乌海市| 晋江市| 香格里拉县| 额济纳旗| 亳州市| 安溪县| 芜湖县| 临朐县| 台前县| 上虞市| 浦江县| 井陉县| 察隅县|