2011-12-14 41 views
1

我想將bean類轉換爲映射(key =成員的名稱,value =成員的值)。BeanUtils - 'describe'方法返回錯誤的數組值

我使用BeanUtils.describe(beanClass)方法;

編輯:我使用的commons-BeanUtils的1.8.3,JDK 1.6.0_20,於commons-BeanUtils的1.5它的作品我)

的問題是,返回值是不正確,(地圖僅包含從數組中第一項),

的代碼:

public class Demo { 

     private ArrayList<String> myList = new ArrayList<String>(); 

     public Demo() { 
      myList.add("first_value"); 
      myList.add("second_value"); 
     } 

     public ArrayList<String> getMyList() { 
      return myList; 
     } 

     public void setMyList(ArrayList<String> myList) { 
      this.myList = myList; 
     } 

     public static void main(String[] args) { 
      Demo myBean = new Demo(); 
      try { 
       Map describe = BeanUtils.describe(myBean); 
       Iterator it = describe.entrySet().iterator(); 
       while (it.hasNext()) { 
        Map.Entry pairs = (Map.Entry) it.next(); 
        System.out.println(String.format("key=%s,value=%s", (String) pairs.getKey(), (String) pairs.getValue())); 

       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
  • 預期輸出:

鍵= myList中,值= [FIRST_VALUE,SECOND_VALUE]

鍵=類,值=類$演示

  • 但真正的輸出是:

鍵= myList,value = [first_value]

key = class,value = class $ Demo

正如你所看到的數組包含兩個值,但輸出(和地圖)只包含一個,爲什麼?

感謝,

回答

0

我在我的電腦上運行你的代碼示例,輸出是: 鍵= myList中,值= [FIRST_VALUE,SECOND_VALUE] 鍵= A類,值= A類COM .gpdi.infores.dao.test.Demo 使用JDK5或更高版本即可。

+0

感謝您的回覆, 我試着用commons-beanutils 1.5來運行代碼,它可以工作。 1.8.3中可能有一個錯誤。 – user1097157 2011-12-14 10:21:11