Jackson自定义反序列化程序仅获取列表XML中的最后一个值

2022-03-02 00:00:00 deserialization java jackson

我有以下XML

<root>
<date>112004</date>
<entries>
    <entry id = 1>
        <status>Active</status>
        <person>
            <Name>John</Name>
            <Age>22</Age>
        </person>
    </entry>
    <entry id = 2>
        <status>Active</status>
        <person>
            <Name>Doe</Name>
            <Age>32</Age>
        </person>
    </entry>
    <entry id = 3>
        <status>N/A</status>
    </entry>
</entries>

我使用定制的Jackson反序列化程序来获取值,POJO看起来像

@JacksonXmlRootElement(localName="root", namespace="namespace")
类根目录 { 私有字符串date;

@JacksonXmlProperty(localName = "entries", namespace="tns")
private List<Entry> entries;
//getter and setter

}

class Entry {
 private String id;
 private String status;
 private Person person;
 //getter and setter
}

反序列化程序代码如下

public class DeSerializer extends StdDeserializer<root>
{
 protected DeSerializer() {
    super(root.class);
  }
  @Override
public root deserialize(JsonParser p, DeserializationContext ctxt)  throws IOException, JsonProcessingException {
    JsonNode nodes = p.readValueAsTree();
    ObjectMapper mapper = new ObjectMapper();
    List<Entry> entry = mapper.convertValue(nodes.findValues("entry"), new TypeReference<List<Entry>>() {});
}
}

main()
{
XmlMapper x = new XmlMapper();
 final SimpleModule module = new SimpleModule("configModule",   com.fasterxml.jackson.core.Version.unknownVersion());
            module.addDeserializer(root.class, new DeSerializer());
            x.registerModule(module);
            root r = x.readValue(xmlSource, root.class); /*xmlsource is xml as string*/
}

问题是,当我调试时,我总是从XML获取条目的最后一个值。所以(在反序列化程序中)节点的值是{"Date":"112004","Entries":{"Entry":{"id":"3","Status":"N/A"},我不知道为什么它没有被视为列表。我确实为List添加了UNWARTED=FALSE的批注,但没有成功。


解决方案

似乎readValueAsTree不支持提取整个集合。

我在没有自定义的情况下做了一些工作DeSerializer可以正常工作。

@JacksonXmlRootElement(localName="root")
public class Root {
@JacksonXmlElementWrapper(useWrapping = true)
private List<Entry> entries;

private String date;

public List<Entry> getEntries() {
    return entries;
}

public void setEntries(List<Entry> entries) {
    if (this.entries == null){
        this.entries = new ArrayList<>(entries.size());
    }
    this.entries.addAll(entries);
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}
}

class Entry {
private String id;
private String status;
private Person person;
}

class Person {
@JacksonXmlProperty(localName = "Name")
private String name;

@JacksonXmlProperty(localName = "Age")
private String age;
}

然后单元测试:

    @Test
    void test_xml_XmlMapper() throws Exception {
    JacksonXmlModule xmlModule = new JacksonXmlModule();
    xmlModule.setDefaultUseWrapper(false);
    ObjectMapper xmlMapper = new XmlMapper(xmlModule);

    String xmlContent = "your xml file here"
    Root bean = xmlMapper.readValue(xmlContent, Root.class);
    assertThat(bean.getEntries().size(), Matchers.equalTo(3));
}

相关文章