簡體   English   中英

基於常規枚舉的jaxb枚舉-解組問題

[英]jaxb enum based on regular enum - unmarshall issues

我面臨一個問題,希望有人能幫助我。 我有一個模型項目,其中包含很多pojos和一些枚舉。

我有一個通用地圖,其中包含鍵和值,可以是任何類型。 該地圖如下所示:

@XmlRootElement
public class Foo implements Serializable
     Map<Object,Object> myMap

映射可以保存的值之一是枚舉。 由於我想讓jaxb編組/解組,所以我試圖創建類似

@XmlEnum(value=org.yyy.models.enum.FooEnum)
public class MyEnum

枚舉類是一個簡單的枚舉:

public enum FooEnum{
    ONE,TWO,THREE
}

由於我不想使用@XmlEnumValue復制枚舉的值,因此我想知道如何添加該依賴項。 同樣,無需維護兩組值(一組在枚舉中,一組在我的jaxb枚舉中)。

在我看到的所有示例中,它都非常簡單,因為在通常情況下,該類包含某種類型的成員,因為該映射可以容納任何值,所以我無法對其添加任何限制。

我的問題是jaxb解組,看來它無法將測試中的值轉換為枚舉值-它不會引發異常,解組值為null

這是示例:

    <table>
    <entry>
        <key xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">Name</key>
        <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">Test</value>
    </entry>
    <entry>
        <key xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">Type</key>
        <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:type="myEnum">ONE</value>
    </entry>
</table>

看起來您幾乎一切都正確,但是您的問題是上下文可能對如何處理您定義的枚舉類一無所知。

我能夠構造一小組類來生成所需的輸出,而無需任何特殊的枚舉注釋。

編輯 :因此,在進一步評估了與解組有關的問題之后,我修改了測試以嘗試解組粘貼在問題描述中的XML(用<Foo></Foo>標記包裝),然后重新將獲得的對象編組為System.out驗證一切正常。 我創建了一個名為“ MyXml.xml”的文件,其內容如下(從上面):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Foo>
    <table>
        <entry>
            <key xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">Name</key>
            <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">Test</value>
        </entry>
        <entry>
            <key xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">Type</key>
            <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:type="myEnum">ONE</value>
        </entry>
    </table>
</Foo>

然后,使用如下注釋的Foo類:

@XmlRootElement(name = "Foo")
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo implements Serializable {
    private static final long serialVersionUID = 1L;

    public Foo() {}

    // wrap your map in a table tag
    @XmlElementWrapper(name = "table")
    // the entry will be the tag used to enclose the key,value pairs
    @XmlElement(name="entry")
    Map<Object, Object> myMap = new HashMap<Object, Object>();

    public Map<Object,Object> getMyMap() {
        return myMap;
    }
}

簡單的枚舉類,不需要注釋:

public enum MyEnum {
    ONE, TWO, THREE;
}

此測試:

public class Test {

    public static void main(String[] args) throws Exception {
        // create your context, and make sure to tell it about your enum class
        JAXBContext context = JAXBContext.newInstance(new Class[]{Foo.class,MyEnum.class});
        // create the unmarshaller
        Unmarshaller unmarshaller = context.createUnmarshaller();
        // try to unmarshal the XML into a Foo object
        Foo f = (Foo) unmarshaller.unmarshal(new File("MyXml.xml"));

        // if it worked, try to write it back out to System.out and verify everything worked!
        if ( f != null) {
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.marshal(f, System.out);
        }        
    }
}

產生以下輸出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Foo>
    <table>
        <entry>
            <key xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">Type</key>
            <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="myEnum">ONE</value>
        </entry>
        <entry>
            <key xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">Name</key>
            <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">Test</value>
        </entry>
    </table>
</Foo>

如您所見,不需要額外的枚舉管理,並且觀察到正確的輸出。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM