使用 JAXB 解组为 POJO 后列表返回为 null

List coming back as null after unmarshalling into a POJO using JAXB

我正在尝试使用 JAXB 将 XML 解组为 Java POJO,除列表(listId1 和 listId2)外,所有元素都很好地解组,下面是 xml 、pojo classes 和业务 class.

<?xml version="1.0" encoding="UTF-8"?>
<abacus-1.0-snapshot xmlns="urn:xxxxxxx">
    <rules>
        <rule>
            <name>rule1</name>
            <errorType>WARNING</errorType>
            <flag>true</flag>
            <startDate>2020-05-19</startDate>
            <listId1>
                <id>100101</id>
                <id>100102</id>
            </listId1>
            <listId2>
                <id>100103</id>
                <id>100104</id>
            </listId2>
        </rule>
        <rule>
            <name>rule2</name>
            <errorType>ERROR</errorType>
            <flag>false</flag>
            <startDate>2020-05-20</startDate>
            <listId1>
                <id>100105</id>
                <id>100106</id>
            </listId1>
            <listId2>
                <id>100107</id>
                <id>100107</id>
            </listId2>
        </rule>
    </rules>
</abacus-1.0-snapshot>

元素映射到 Rules.java

的 Rules.java
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "rules", namespace = "urn:xxxxxxx")
public class RulesPOJO
{

    @XmlElement(name = "rule", namespace = "urn:xxxxxxx")
    private final List<rulePOJO> rulesPOJO = new ArrayList<rulePOJO>();

    public List<rulePOJO> getRulesPOJO()
    {
        return rulesPOJO;
    }
}

Rule.java class 用于将 元素映射到 Rule.java

@XmlAccessorType(XmlAccessType.NONE)
public class RulePOJO
{

    @XmlElement(namespace = "urx:xxxxx")
    private final String name = null;
     
    @XmlElement(namespace = "urx:xxxxx")
    private final String errorType = null;
    
    @XmlElement(namespace = "urx:xxxxx")
    private final Date startDate = null;
    
    @XmlElement(name = "listId1", namespace = "urx:xxxxx")
    private final List<Long> listId1 = new ArrayList<>();
    
    @XmlElement(name = "listId2", namespace = "urx:xxxxx")
    private final List<Long> listId2 = new ArrayList<>();

    public String getName()
    {
        return name;
    }

    public errorType getErrorType()
    {
        return errorType;
    }

    public Boolean getFlag()
    {
        return flag;
    }

    public Date getStartDate()
    {
        return startDate;
    }

    public List<Long> getListId1()
    {
        return listId1;
    }

    public List<Long> getListId2()
    {
        return listId2;
    }
}

业务 class 用于解组 xml 到 pojo

public class Retriever() throws JAXBException
    {
        Document document = Configuration.load(URI.create("urn:xxxxxx"));

        Unmarshaller unmarshaller = JAXBContext.newInstance(RulesPOJO.class).createUnmarshaller();
        RulesPOJO rulesPOJO = (RulesPOJO) unmarshaller
                .unmarshal(document.getElementsByTagName("Rules").item(0));
        for(RulePOJO rulePOJO : RulesPOJO.getRulesPOJO())
        {
            // the following are coming back null
            List<Long> listId1 = rulePOJO.getListId1();
            List<Long> listId2 = rulePOJO.getListId2();
        }
    }

谢谢!

正如评论中所讨论的,id 是被视为数组列表的重复元素,因此在给定的 link https://howtodoinjava.com/jaxb/xmlelementwrapper-annotation/ 中提到。您可以使用 XMLElementWrapperXMLElement 类型的注释来表示包装器和子元素。