posts - 122,  comments - 25,  trackbacks - 0
          XFire開發(fā)時(shí),在返回?cái)?shù)據(jù)類型時(shí)遇到了一些麻煩,查到這樣一篇文單,非常不錯(cuò),故收藏之。

          翻譯: zilong3927  原文地址: http://docs.codehaus.org/display/XFIRE/Mapping+collections

          調(diào)用 Web Services 時(shí),經(jīng)常需要返回集合( collection )作為結(jié)果,或者接受 collection 型的參數(shù)。 SOAP 本身就支持這一點(diǎn)。

          但是這一機(jī)制的問題在于, java 語言的 collections 是無類型的( untyped . 因此,如果要在 Java 1.4 當(dāng)中支持 collections 就需要做一些額外的工作。

          Java 5 & 范型( Generics

          首先而且是推薦的做法是在 JDK5 當(dāng)中使用范型( generics )。范型能夠使你在代碼當(dāng)中為你的 collections 指定類型信息, 從而允許 xfire 自動(dòng)地推導(dǎo)出 collection 類型,生成正確的 wsdl 等等。

          下面示例了如何寫這樣的一個(gè)方法:

          public   Collection<  String  > getValuesForIds(Collection<  Integer  >);

          Java 1.4 & 集合( Collections

          有些情況下并不總能夠使用范型( generics . 例如,如果你的部署環(huán)境使用 JDK 1.4 或者你想暴露一些遺留的服務(wù),而同時(shí)又不打算修改任何代碼也不打算進(jìn)行移植。

          對(duì)于這樣的一些情況而言, 你需要生成一個(gè) xml 映射文件,來指定方法和它們對(duì)應(yīng)的集合類型( collection types .

          這個(gè) xml 文件的名字必須是 <className>.aegis.xml 其中 className 是你的服務(wù)( service )的接口類( unqualified class )的名字。

          下面最好通過一個(gè)例子來展示這個(gè) xml 文件的格式。 我們想要展現(xiàn)的服務(wù)有這樣的一個(gè)接口 :

          public interface MyService1{
              String getFoo();
              Collection getCollection();
              
          void setList( int id , java.util.List);
          }

          既然代碼中的 collections 沒有指定類型, 我們劇需要生成一個(gè) xml 文件來指定所需要的類型。 這個(gè)文件的路徑應(yīng)該和 MyService1.class 在同一個(gè)包( package )當(dāng)中, 并且它的名字應(yīng)該是 MyService1.aegis.xml

          對(duì)于這個(gè)接口來說,一個(gè)最簡(jiǎn)單的映射文件如下 :

           <mappings>
              
          <mapping>
                  
          <method name= "getCollection" >
                      
          <return-type componentType= "java.lang.String" />
                  
          </method>
                  
          <method name= "setList" >
                      
          <parameter index= "1" componentType= "java.lang.String" />
                  
          </method>
              
          </mapping>
          </mappings>

          注意這個(gè)映射文件確切地指定了所需要的信息,不包含任何冗余。 例如, getFoo 方法沒有被指定,這是由于它沒有包含任何 collections ,因此能夠在沒有任何映射信息的情況下暴露給使用者。

          其次, setCollection 方法沒有指定索引為 0 的參數(shù)。 這是由于該參數(shù)類型為 int ,因此不需要任何映射

          如果我們有多個(gè)方法,都匹配指定的映射又該怎么辦 ? 這種情況下, 映射就對(duì)所有匹配的方法均有效。

          所以,如果在我們的接口中增加以下的方法:

          void setList(int id ,java.util.List,boolean persist);

          那么現(xiàn)在我們的映射定義對(duì)于兩個(gè) setList 方法都有作用。這種情況下, 我們不必為額外的參數(shù)(譯者注:此處指 boolean persist )指定兩次映射 . 映射文件就指定了所有那些第二個(gè)參數(shù)為 List 的方法,并假定 List 中包含的都是 strings

          如果我們想讓那個(gè)具有 3 個(gè)參數(shù)的方法,其中的 list 不包含 Strings 而是實(shí)際上包含 Dates? 這種情況下, 就需要一個(gè)更確切的映射來覆蓋( override )原先那個(gè)更一般的, 所以我們的映射文件需要添加下面這個(gè)定義 :

                  <method name=  "setList"  >
                      
          <parameter index= "1" componentType= "java.lang.String" />
                      
          <parameter index= "2" class= "boolean" />
                  
          </method>

          注意一下類型屬性。 現(xiàn)在這個(gè)映射將對(duì)所有那些第二個(gè)參數(shù)為 List ,第三個(gè)參數(shù)為 boolean 型的方法適用。 在我們的接口當(dāng)中,這個(gè)映射唯一地確定了一個(gè)特定的方法,使用這個(gè)映射就能夠解釋方法當(dāng)中的 List 參數(shù)。

          在優(yōu)先順序方面, 更確切的映射總是優(yōu)先于更一般的。

          讓我們考慮下面這個(gè)復(fù)雜一些的例子 :

          public    interface   MyService2
          {
              Collection getCollection(); 
          //method 1
              Collection getCollection( int id); //method 2
              Collection getCollection( String id); //method 3
              Collection getCollectionForValues( int value , Collection c); //method 4
              Collection getCollectionForValues( String id , Collection c); //method 5
          }

          映射文件的內(nèi)容為 :

           <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>

          這個(gè)文件的格式是不需要做過多解釋的。但有幾點(diǎn)還是需要加以說明。

          先來看一下第一個(gè)映射 (mapping 1) 這個(gè)映射指定了所有 getCollection 方法所返回的 collections contain 均包含 java.lang.Doubles 如果沒有指定其他的 getCollection 映射, 那么這個(gè)映射將對(duì)方法 1 2 3 都適用。

          但是,第二個(gè)映射更加明確地指定了它所適用的方法。即如果 getCollection 方法的第一個(gè)參數(shù)是 int 型,那么該方法所返回的 collection 包含的是 Float 型。 由于這條規(guī)則更加明確,它將為方法 2 覆蓋掉第一個(gè)映射,這是滿足映射約束標(biāo)準(zhǔn)的。

          使用以上的規(guī)則,不難推導(dǎo)出方法 4 和方法 5 返回的 collections 結(jié)果的組件類型( component types )。

          Collections on Javabeans

          對(duì)于使用 collections java beans 來說,語法也是類似的。 例如,比方說我們有一個(gè) Company bean ,包含了一個(gè) List 其中的對(duì)象是 employees:

           public class Company
          {
              
          private Collection employees;
              Collection getEmployees() { 
          return employees; }
              
          public void setEmployees(Collection employees) { this .employees = employees };
          }

          除了可以使用 <method> & <parameter> 元素外, 也可以使用 <property> 元素 :

           <mappings>
            
          <mapping>
               
          <property componentType= "org.codehaus.xfire.Employee" />
            
          </mapping>
          </mappings>

          Handling Maps

          Java Maps 并不能很好地映射到 XML Schema (no pun intended) ,因?yàn)?/span> XML Schema 中沒有 Map 的概念,客戶端也是這樣, Maps 被轉(zhuǎn)換成 {key value} 元組的集合。 除了要提供 value 的類型以外,你還必須為 Aegis 提供 key 的類型 :

           public   class GiftService {
                Map getGiftList() { 
          /* returns a map of NiceChild => Present */ }
           }

          映射文件應(yīng)該像下面這樣 :

           <mappings>
            
          <mapping>
              
          <method name= "getGiftList" >
                
          <return-type keyType= "org.codehaus.xfire.NiceChild" componentType= "org.codehaus.xfire.Present" >
              
          </method>
            
          </mapping>
          </mappings>

          這將生成下面的類型 :

           <xsd:complexType name=  "NiceChild2PresentMap"  >
            
          <xsd:sequence>
              
          <xsd:element name= "entry" minOccurs= "0" maxOccurs= "unbounded" >
                
          <xsd:complexType>
                  
          <xsd:sequence>
                    
          <xsd:element name= "key" type= "ns1:NiceChild" minOccurs= "0" maxOccurs= "1" />
                    
          <xsd:element name= "value" type= "ns1:Present" minOccurs= "0" maxOccurs= "1" />
                  
          </xsd:sequence>
                
          </xsd:complexType>
              
          </xsd:element>
            
          </xsd:sequence>
          </xsd:complexType>

          Collections of Collections of Collections of....

          在某些情況下,你可能想要傳遞 Collections of Collections 。比方說你有一個(gè)返回 List of a List of Doubles 的服務(wù) ( 不要問為什么你要做這樣一件事情 ...):

          public class ListService
          {
            
          public List getListOfListOfDoubles
            {
              List l 
          = new ArrayList();
              List doubles 
          = new ArrayList();
              doubles.add( 
          new Double (1.0));
              l.add(doubles);
              
          return l;
            }
          }

          要處理這種情況,我們需要引進(jìn)一個(gè)新的 <component> 元素。 下面是一個(gè)很好的例子 :

           <mappings>
            
          <mapping>
              
          <method name= "getListofListofDoubles" >
                
          <return-type componentType= "#someDoubles" />
              
          </method>
              
          <component name= "someDoubles" class= "java.util.List" componentType= "java.lang.Double" />
            
          </mapping>
          </mappings>

          正像你在這里所看到的,返回類型的 componentType 是一個(gè)指向 <component> 的引用,而不是一個(gè)類。組件類型 "#someDoubles" 引用到名字為 "someDoubles" <component>

          Aegis 將會(huì)自動(dòng)給這些 collections 命名為 ArrayOfDouble ArrayOfArrayOfDouble 你也可以改變這些名字。 要設(shè)置你自己的名字, 提供一個(gè) "typeName" 屬性即可 :

           <mappings>
            
          <mapping>
              
          <method name= "getListofListofDoubles" >
                
          <return-type componentType= "#someDoubles" typeName= "LotsOfDoubles" />
              
          </method>
              
          <component name= "someDoubles" class= "java.util.List" typeName= "SomeDoubles" componentType= "java.lang.Double" />
            
          </mapping>
          </mappings>

          posted on 2007-04-20 18:36 josson 閱讀(2448) 評(píng)論(0)  編輯  收藏 所屬分類: java 開發(fā)
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 新和县| 中西区| 新宾| 巴青县| 伊金霍洛旗| 敖汉旗| 梁平县| 若羌县| 崇礼县| 高密市| 建平县| 如东县| 鄯善县| 九寨沟县| 宜昌市| 邳州市| 平武县| 镇江市| 炉霍县| 长岭县| 蕉岭县| 浙江省| 枣阳市| 乳源| 盱眙县| 兴化市| 罗平县| 沙湾县| 冀州市| 辽阳县| 滕州市| 郓城县| 奎屯市| 兰州市| 临洮县| 思茅市| 田阳县| 丰顺县| 汝阳县| 象山县| 银川市|