JAXB - 枚举注释允许获取值,因为int和string都会导致异常

时间:2015-10-31 11:34:07

标签: java xml-parsing jaxb mule

我希望能够获得枚举(默认)的int值以及文本值(例如2或“TestTwo”)。我试过下面的代码:

在父母:

@XmlElement(name="ProjectType")
private ProjectType projectType;

在Enum中:

@XmlRootElement(name = "Proj")
@XmlType
@XmlEnum(Integer.class)       //Which class to set here??
public enum ProjectType implements java.io.Serializable {

 @XmlEnumValue("2") TestTwo(2),
 @XmlEnumValue("3") TestThree(3);

 private int projectType = 0;

    private ProjectType(int projectType) {
        this.projectType = projectType;
    }
}

但是当我运行mule项目时会遇到异常:

Caused by: java.lang.ExceptionInInitializerError
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~    [?:1.8.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_51]
at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_51]
at java.lang.Class.getEnumConstantsShared(Class.java:3320) ~[?:1.8.0_51]
at java.lang.System$2.getEnumConstantsShared(System.java:1249) ~[?:1.8.0_51]
at java.util.EnumMap.getKeyUniverse(EnumMap.java:752) ~[?:1.8.0_51]
at java.util.EnumMap.<init>(EnumMap.java:138) ~[?:1.8.0_51]

Caused by: java.lang.NullPointerException
at com.test.demo.ProjectType.values(ProjectType.java:1) ~[?:?]
at com.test.demo.ProjectType.<init>(ProjectType.java:22) ~[?:?]
at com.test.demo.ProjectType.<clinit>(ProjectType.java:13) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_51]
at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_51]
at java.lang.Class.getEnumConstantsShared(Class.java:3320) ~[?:1.8.0_51]
at java.lang.System$2.getEnumConstantsShared(System.java:1249) ~[?:1.8.0_51]
at java.util.EnumMap.getKeyUniverse(EnumMap.java:752) ~[?:1.8.0_51]
at java.util.EnumMap.<init>(EnumMap.java:138) ~[?:1.8.0_51]

我看不出这里有什么问题?

1 个答案:

答案 0 :(得分:1)

使用像这样的父类

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Elem {
    @XmlElement(name="ProjectType")
    private ProjectType projectType;
    //...
}

和你发布的枚举,编组没问题:

<elem>
    <ProjectType>2</ProjectType>
</elem>

如果我将枚举更改为

@XmlRootElement(name = "Proj")
@XmlType
@XmlEnum(String.class)
public enum ProjectType {
  @XmlEnumValue("II")      // or @XmlEnumValue(2)
  TestTwo(2),
  @XmlEnumValue("III")      // or @XmlEnumValue(3)
  TestThree(3);
  // ....
}

输出变为

<elem>
  <ProjectType>II</ProjectType>
</elem>

我认为JAXB没有特别的问题。 修改添加了主类。

public class Main {
    private static final String XMLIN   = "hello.xml";

    void marshal() throws Exception {
        Elem root = new Elem();
    root.setProjectType( ProjectType.TestTwo );
        JAXBContext jc = JAXBContext.newInstance( Elem.class );
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal( root, System.out );
    }

    void unmarshal() throws Exception {
        JAXBContext jc = JAXBContext.newInstance( Elem.class );
        Unmarshaller u = jc.createUnmarshaller();
        Elem elem = (Elem)u.unmarshal( new File( XMLIN ) );
        System.out.println( "elem projectType " + elem.getProjectType() );
    }

    public static void main( String[] args ) throws Exception {
        Main main = new Main();
    main.marshal();
        main.unmarshal();
    }
}