gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:59:17 24 4
gpt4 key购买 nike

我希望能够获取枚举的 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类。

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

关于java - JAXB - 枚举注释允许获取值,因为 int 和 string 都会导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33451067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com