有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java jackson自定义反序列化程序仅获取列表xml中的最后一个值

我有以下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")

类根 { 私人字符串日期

@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中获取最后一个值。所以节点(在反序列化程序中)的值是{“日期”:“112004”,“条目”:{“条目”:{“id”:“3”,“状态”:“N/A”}}},我不确定为什么它不被视为列表。我确实为列表添加了unwrapped=false的注释,但没有成功


共 (2) 个答案

  1. # 1 楼答案

    似乎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));
    }
    
  2. # 2 楼答案

    如果仍有人面临此问题,请升级至最新的jackson 2.12.3版本。 我在2.11中遇到了这个问题。x版