聽風的歌

          欣賞永運比跟風好

          ◎XFire 使用記

          XFire開發Web服務的基本步驟
          1) 檢驗JAVA類的方法和構造函數是否是公共的,一定要是公開的。
          2) 將XFire Servlet相關的入口添加到web.xml中。

          <servlet>
              
          <servlet-name>XFireServlet</servlet-name>
              
          <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
              
          <load-on-startup>0</load-on-startup>
            
          </servlet>
            
          <servlet-mapping>
              
          <servlet-name>XFireServlet</servlet-name>
              
          <url-pattern>/services/*</url-pattern>
            
          </servlet-mapping>

          3) 創建services.xml并把它放到WEB-INF/classes/META-INF/xfire目錄下,這是默認的做法;也可以將services.xml的路徑配置到web.xml文件中。

          <servlet>
              
          <servlet-name>XFire</servlet-name>
              
          <display-name>XFire Servlet</display-name>
              
          <servlet-class>
                  org.codehaus.xfire.transport.http.XFireConfigurableServlet
              
          </servlet-class>

              
          <!--
                The servlet will by default look for the configuration on
                the classpath in "META-INF/xfire/services.xml". You can
                override it with this parameter. Seperate multiple configuration files with a comma.
               
          -->
             
          <!-- 默認會在classpath的META-INF/xfire/下查找services.xml文件,
               可以覆蓋這個參數,指定多個配置文件
          -->
              
          <init-param>
                
          <param-name>config</param-name>
                
          <param-value>services.xml</param-value>
              
          </init-param>
            
          </servlet>

          4) 將XFire和其它第三方庫添加到Web應用的WEB-INF/lib目錄下。


          FAQ
          1) Weblogic8.1中應用XFire,啟動wls時,出現java.lang.NoSuchMethodError的解決辦法。
             首先,將XFire提供的QName JAR(qname.jar)放進WEB-INF/lib目錄下。然后,將weblogic.xml文件放到WEB-INF下。
          weblogic.xml的內容為:

          <!DOCTYPE weblogic-web-app PUBLIC
            "-//BEA Systems, Inc.//DTD Web Application 8.1//EN"
            "http://www.bea.com/servers/wls810/dtd/weblogic810-web-jar.dtd"
          >

          <weblogic-web-app>
            
          <container-descriptor>
              
          <prefer-web-inf-classes>true</prefer-web-inf-classes>
            
          </container-descriptor>
          </weblogic-web-app>

          具體的詳細解說,請看XFire的User's Guide.


          技巧
          1. 如果Web Services的方法的參數是Collections,那就需要建一個Mapping文件,而且Mapping文件的命名為className.aegis.xml,并且與class放在同一個package下。
          如:
          public interface IConstraceSerice {
             boolean editConstraceInfo(List aList);
          }
          Mapping文件如下:IConstraceService.aegis.xml
          <?xml version="1.0" encoding="utf-8"?>
          <mappings>
             <mapping>
                <method name="editConstraceInfo">
                    <parameter index="0" componentType="java.lang.String"/>
                </method>
             </mapping>
          </mappings>

          <parameter index="0" componentType="java.lang.String"/>表示第一個參數,里面實際值的類型,這里實際值的類型是java.lang.String.
          如果是一個JavaBean,如com.test.TestBean,那以就要寫成<parameter index="0" compentType="com.test.TestBean"/>

          2. 如果返回類型是List或Map,并且里面存放的是自定義類的話,則需要增加一個對于服務接口的配置文件。該文件的命名規則是 接口文件名.aegis.xml。例如接口是UserService.java的話,則此配置文件命名為UserService.aegis.xml。注意此配置文件須與接口放在同一目錄下面。
          <?xml version="1.0" encoding="UTF-8"?> 
           <mappings> 
             <mapping > 
               <method name="getUsers"> 
                 <return-type componentType="com.test.domain.User"/> 
               </method> 
             </mapping> 
           </mappings>
          getUsers方法返回類型是List,里面裝的User對象。對于這種類型的方法,在配置文件中描述了它的返回值類型。

          如果返回的類型是Map的話,做法和List一樣。但定義的類型,是Map中的Value部分,并且這樣的話,Map中Value所存放的對象就必須全部是同一種類啦。

          下面給出一個詳細的例子:
          1)服務接口:
          public interface MyService2
          {
              boolean getInfo();
              Collection getCollection(); //method 1
              Collection getCollection(int id); //method 2
              Collection getCollection(String id); //method 3
              Collection getCollectionForValues(String id, Collection c); //method 4
              Collection getCollectionForValues(int value, Collection c); //method 5
          }
          2) Mapping文件內容:
          <mappings>
              <mapping>
                  <!-- mapping 1 -->
                  <method name="getCollection">
                      <return-type componentType="java.lang.Double"/>
                  </method>
                  <!-- mapping 2 -->
                  <method name="getCollection">
                      <return-type componentType="java.lang.Float"/>
                      <parameter index="0" class="int"/>
                  </method>
                  <!-- mapping 3 -->
                  <method name="getCollectionForValues">
                      <return-type componentType="java.math.BigDecimal"/>
                  </method>
                  <!-- mapping 4 -->
                  <method name="getCollectionForValues">
                      <parameter index="0" class="java.lang.String"/>
                      <parameter index="1" componentType="java.util.Date"/>
                  </method>
                  <!-- mapping 5 -->
                  <method name="getCollectionForValues">
                      <return-type componentType="java.util.Calendar"/>
                      <parameter index="0" class="int"/>
                      <parameter index="1" componentType="java.lang.Bit"/>
                  </method>
              </mapping>
          </mappings>


          3. 如果一個方法的返回類型是一個JavaBean,而這個JavaBean當中又存在Collections,那么就需要定義一個與JavaBean相關的Mapping文件,文件名要與JavaBean名相同,如:User.aegis.xmll,并且與JavaBean放在同一個目錄.

          例子:
          1) 服務接口
          public interface IYMServiceFacade {
             User getUser();
          }

          2) JavaBean
          public class User {
             private Strirng userName;
             // 這里是一個Collection
             private Set rooms;

             .....
             .....
          }

          3) Mapping文件(User.aegis.xml)
          <?xml version="1.0" encoding="utf-8"?>
          <mappings>
              <mapping>
                  <property name="rooms" componentType="com.powerunion.ymservice.dto.Room"/>
              </mapping>
          </mappings>

          介紹:<property name="rooms" componentType="com.powerunion.ymservice.dto.Room"/>
          其中的name屬性就是JavaBean里面定義的rooms,componentType上面的相同,表示Collections里真正存儲的類型.


          注:如果服務接口的參數或者返因類型是基本類型(int, float, double等)就不需要創建Mapping文件。
          詳細的信息見XFire的User's Guide.


          4.如果Web Services和客戶端運行在同一個JVM上,可以選擇使用本地傳輸,可以大幅提升性能。如:以下指定服務端URL的這行。
          String serviceUrl = "http://localhost:8080/YM/services/ContractService";
          替換為
          String serviceUrl = "xfire.local://ContractService";


          5. 用ant task 生成xfire 客戶端代碼
            
          用xfire不管是配置web service,還是生成客戶代碼都很方便.
          生成客戶代碼只要在用ant生成就可以了!

          build.xml


          代碼
          <?xml version="1.0"?> 
           
          <project default="genfiles" basedir="."> 
              <property name="lib" value="WebRoot/WEB-INF/lib" /> 
              <path id="myclasspath"> 
                  <fileset dir="${lib}"> 
                      <include name="*.jar" /> 
                  </fileset> 
                  <pathelement location="${genfiles}" /> 
              </path> 
              <property name="code_path" value="src" /> 
              <property name="wsdl_path" value="Echo1Service.xml" /> 
              <property name="code_package" value="com.client" /> 
              <target name="genfiles"  description="Generate the files"> 
                  <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="myclasspath" /> 
                  <wsgen outputDirectory="${code_path}" wsdl="${wsdl_path}" package="${code_package}" binding="xmlbeans" /> 
              </target> 
          </project> 
           

          posted on 2007-06-19 10:26 聽風的歌 閱讀(3500) 評論(0)  編輯  收藏 所屬分類: SOA & WS


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


          網站導航:
           

          導航

          <2007年6月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 牡丹江市| 胶南市| 临漳县| 潜山县| 静安区| 保德县| 拉孜县| 垦利县| 广安市| 仁寿县| 河北省| 平遥县| 青州市| 青神县| 古浪县| 甘泉县| 乐山市| 淮阳县| 叙永县| 巴塘县| 新泰市| 肥西县| 专栏| 钦州市| 阳原县| 读书| 秭归县| 龙里县| 池州市| 左贡县| 肇源县| 泽库县| 鄂托克旗| 息烽县| 乌兰县| 库伦旗| 集贤县| 石城县| 沅陵县| 从化市| 梁山县|