【问题标题】:jaxb enum based on regular enum - unmarshall issues基于常规枚举的 jaxb 枚举 - 解组问题
【发布时间】:2014-07-24 07:35:30
【问题描述】:

我遇到了一个问题,我希望有人可以帮助我。 我有一个模型项目,其中包含很多 pojo,还有一些枚举。

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

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

【问题讨论】:

    标签: java enums jaxb


    【解决方案1】:

    看起来你几乎一切都正确,但你的问题是上下文可能不知道如何处理你定义的枚举类。

    我能够构建一小组类来生成您想要的输出,而无需任何特殊的枚举注释。

    编辑:因此,在进一步评估问题后,特别是与解组相关的问题,我修改了测试以尝试解组粘贴在您的问题描述中的 XML(用 &lt;Foo&gt;&lt;/Foo&gt; 标签包装)并重新-将获得的对象编组到 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>
    

    如您所见,不需要额外的 Enum 管理,并且观察到了正确的输出。 希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-10
      • 2011-07-09
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-23
      • 2011-04-23
      相关资源
      最近更新 更多