2014-07-24 138 views
0

我面临一个问题,我希望有人能帮助我。 我有一个模型项目,其中包含很多pojos,还有一些枚举。基于常规枚举的jaxb枚举 - unmarshall问题

我有一个通用的地图,其保持键和值,其可以是任何类型的。 地图上看起来是这样的:

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

回答

1

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

我能够构建一个小的组类来生成所需的输出没有任何特殊的枚举注释。

编辑:所以,经过进一步评估的问题,特别是涉及到解组,我修改了测试,试图解组在你的问题描述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> 

正如你所看到的,没有额外的枚举管理需要,并观察到正确的输出。 希望这有助于。