2015-10-19 47 views
1

我想達到以下XML使用的字段類型:FasterXML系列化沒有XML

<model> 
    <entry> 
     <key>A</key> 
     <value>1</value> 
    </entry> 
    <entry> 
     <key>B</key> 
     <value>2</value> 
    </entry> 
</model> 

最近POJO模型我已經通過我的代碼試驗得到如下:

import com.fasterxml.jackson.annotation.JsonRootName; 
import com.fasterxml.jackson.core.JsonProcessingException; 
import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.dataformat.xml.XmlMapper; 
import static com.google.common.collect.Lists.newArrayList; 
import java.util.ArrayList; 
import java.util.List; 
import static org.hamcrest.MatcherAssert.assertThat; 
import static org.hamcrest.Matchers.equalTo; 
import org.junit.Test; 

public class InsPersonFormTest { 

    @JsonRootName("model") 
    public static class Model extends ArrayList<Entry> { 

     public Model(List<Entry> entries) { super(entries); } 

     public List<Entry> getEntry() { return this; } 
    } 

    public static class Entry { 

     String key; 
     String value; 

     public Entry(String key, String value) { 
      this.key = key; 
      this.value = value; 
     } 

     public String getKey() { return key; } 

     public String getValue() { return value; }  
    } 

    @Test 
    public void shouldSendPostRequest() throws JsonProcessingException { 
     Model model = new Model(newArrayList(new Entry("A", "1"), new Entry("B", "2"))); 

     ObjectMapper xmlMapper = new XmlMapper(); 
     String xml = xmlMapper.writeValueAsString(model); 

     assertThat(xml, equalTo(
       "<model>" 
       + "<entry><key>A</key><value>1</value></entry>" 
       + "<entry><key>B</key><value>2</value></entry>" 
       + "</model>")); 
    } 
} 

但它給了我

預計:

"<model><entry><key>A</key><value>1</value></entry><entry><key>B</key><value>2</value></entry></model>"

但:是

"<model><item><key>A</key><value>1</value></item><item><key>B</key><value>2</value></item></model>"

我怎樣才能改變itementry或使用simpliest POJO結構更適合Map<String, String>場?

回答

1

請勿讓您的Model類爲ArrayList的子類型。而是使用組成

public static class Model { 
    private ArrayList<Entry> entry; 

    public Model(List<Entry> entries) { 
     entry = entries; // or make a copy 
    } 

    @JacksonXmlElementWrapper(useWrapping = false) 
    public List<Entry> getEntry() { 
     return entry 
    } 

} 

ArrayListList和傑克遜處理的管理方式List

您需要添加JacksonXmlElementWrapper,以便您可以告訴傑克遜不要包裝生成的XML。

然後可以使用

@JacksonXmlRootElement(/* custom */) 

註釋Entry並添加XML節點和命名空間價值的本地名稱。

+0

由於''標記中的雙重包裝,我已經使'Model'類成爲'ArrayList'的子類。如果我完全按照您的建議做了所有事情,那麼不幸的是這種情況會再次出現。下面是一個輸出:''。正如你可以看到''標籤再次翻倍。 – ytterrr

+0

@ytterrr你說得對。爲此增加了解決方案。 –

+0

'@ JacksonXmlElementWrapper'是使它工作的關鍵註釋。謝謝。 – ytterrr

-1

您是否嘗試過註釋Entry類?

@JsonRootName("entry") 
public static class Entry { 

    String key; 
    String value; 

    public Entry(String key, String value) { 
     this.key = key; 
     this.value = value; 
    } 

    public String getKey() { return key; } 

    public String getValue() { return value; }  
} 
+0

謝謝您的參與。是的,我試過了 - 看起來@JsonRootName(「entry」)在這種情況下不起作用。 – ytterrr