我希望能够获得enum的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;
}
}
但当我运行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]
我看不出这里出了什么问题?
使用类似的父类
@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没有什么特别的问题编辑添加了Main类。
公共类Main{private static最终字符串XMLIN="hello.xml";void marshali()引发异常{Elem root=新Elem();root.setProjectType(ProjectType.TestTwo);JAXBContext jc=JAXBContext.newInstance(电子类);Marshaller m=jc.createMarshaller();m.setProperty(Marshaller.JAXB_FORMATED_OUTPUT,true);m.marshall(根,System.out);}void unmarshal()引发异常{JAXBContext jc=JAXBContext.newInstance(电子类);Unmarshaller u=jc.createUnmarshaler();Elem-Elem=(Elem)u.unmarshal(新文件(XMLIN));System.out.println("elem-projectType"+elem.getProjectType());}public static void main(String[]args)throws Exception{Main Main=new Main();main.mashall();main.unmarshal();}}