Java 基于常规枚举的jaxb枚举-解组问题

Java 基于常规枚举的jaxb枚举-解组问题,java,enums,jaxb,Java,Enums,Jaxb,我面临一个问题,我希望有人能帮我解决。 我有一个模型项目,其中包含很多POJO,还有一些枚举 我有一个通用映射,它保存键和值,可以是任何类型。 地图如下所示: @XmlRootElement public class Foo implements Serializable Map<Object,Object> myMap enum类是一个简单的enum: public enum FooEnum{ ONE,TWO,THREE } 因为我不想使用@XmlEnumVa

我面临一个问题,我希望有人能帮我解决。 我有一个模型项目,其中包含很多POJO,还有一些枚举

我有一个通用映射,它保存键和值,可以是任何类型。 地图如下所示:

@XmlRootElement
public class Foo implements Serializable
     Map<Object,Object> myMap
enum类是一个简单的enum:

public enum FooEnum{
    ONE,TWO,THREE
}
因为我不想使用@XmlEnumValue复制枚举的值,所以我想知道如何添加该依赖项。同样,无需维护两组值(一个在enum中,一个在我的jaxb enum中)

在我看到的所有示例中,它都非常简单,因为类通常持有某个类型的成员,在我的例子中,因为映射可以持有任何值,所以我不能对它添加任何限制

我的问题是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(用
标记包装),并将获得的对象重新封送至System.out,以验证所有操作是否有效。 我创建了一个名为“MyXml.xml”的文件,其中包含以下内容(来自上面):

此测试:

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>

类型
一个
名称
试验
如您所见,不需要额外的枚举管理,并且观察到了正确的输出。 希望这有帮助

@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>