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

JAXB - Enum annotation allow get value as both int and string causes exceptions

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

父级:

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

在枚举中:

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

但是当我 运行 骡子项目时出现异常:

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.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.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]

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

像这样parentclass

@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 没有特别的问题。 编辑添加了主要内容class。

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();
    }
}