2010-12-09 66 views
0

我正在使用RESTEasy編寫一個RESTful Web服務並試圖編寫包含HashMap的響應。 Web服務生成JSON或XML。 JSON映射是正確的,但XML映射沒有內容。 RESTEasy @WrappedMap註解應該將地圖編組爲XML。RESTEasy @ WrappedMap

@XmlRootElement(name="Response") 
public class RootResponse { 
    private HashMap<String, String> test; 
    public RootResponse() { 
    test = new HashMap<String, String>(); 
    test.put("Fred", "Wilma"); 
    test.put("Barney", "Betty"); 
    } 
    @XmlElement 
    @WrappedMap(map="test", key="name", entry="spouse") 
    public HashMap<String, String> getTest() { 
    return this.test; 
    } 
} 

JSON輸出:

{ 
    "test": { 
    "Fred": "Wilma", 
    "Barney": "Betty" 
    } 
} 

XML輸出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Response> 
    <test/> 
</Response> 

我得到的結果相同,如果我離開了@WrappedMap註解。

@WrappedMap不適用於屬性嗎?

回答

0

我知道你發佈這個問題已經有一段時間了,但我仍然想分享我最近學到的知識,以防其他人可能偶然發現這個問題。

@WrappedMap根據this documentation僅有助於更改輸出XML中元素的名稱。至於爲什麼你不能讓JAXB輸出來自HashMap的XML,我認爲this page有解釋。

希望這會有所幫助。