JAXB 非法注释在解析 XML 期间引发异常

2022-09-01 02:17:36

这是我的XML文件:

<fields>
    <field mappedField="Num">
    </field>
    
    <field mappedField="Type">      
    </field>    
</fields>

我创建了2个类来解析它(Fields.java和Field.java):

@XmlRootElement(name = "fields")
public class Fields {

    @XmlElement(name = "field")
    List<Field> fields = new ArrayList<Field>();
        //getter, setter
}

public class Field {

    @XmlAttribute(name = "mappedField")
    String mappedField;
    //getter,setter
}

但我得到这个例外:

[INFO] com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
[INFO]  at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:66) ~[na:1.6.0_07]
[INFO]  at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:422) ~[na:1.6.0_07]
[INFO]  at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:270) ~[na:1.6.0_07]

我不明白为什么这个例外会上升。例外情况如下:

JAXBContext context = JAXBContext.newInstance(Fields.class);

我使用JDK 1.6_0.0.7。谢谢。


答案 1

这个例外是由于您的 JAXB (JSR-222) 实现认为有两个事物映射具有相同的名称(字段和属性)。您的使用案例有几个选项:

选项 #1 - 使用@XmlAccessorType注释字段(XmlAccessType.FIELD)

如果要注释字段,则应指定@XmlAccessorType(XmlAccessType.FIELD)

字段.java:

package forum10795793;

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement(name = "fields")
@XmlAccessorType(XmlAccessType.FIELD)
public class Fields {

    @XmlElement(name = "field")
    List<Field> fields = new ArrayList<Field>();

    public List<Field> getFields() {
        return fields;
    }

    public void setFields(List<Field> fields) {
        this.fields = fields;
    }

}

字段.java:

package forum10795793;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Field {

    @XmlAttribute(name = "mappedField")
    String mappedField;

    public String getMappedField() {
        return mappedField;
    }

    public void setMappedField(String mappedField) {
        this.mappedField = mappedField;
    }

}

选项 #2 - 注释属性

缺省访问器类型为 。这意味着缺省情况下,JAXB 实现会将公共字段和访问器映射到 XML。使用默认设置,应为要覆盖默认映射行为的公共访问器添加批注。XmlAccessType.PUBLIC

字段.java:

package forum10795793;

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement(name = "fields")
public class Fields {

    List<Field> fields = new ArrayList<Field>();

    @XmlElement(name = "field")
    public List<Field> getFields() {
        return fields;
    }

    public void setFields(List<Field> fields) {
        this.fields = fields;
    }

}

字段.java:

package forum10795793;

import javax.xml.bind.annotation.*;

public class Field {

    String mappedField;

    @XmlAttribute(name = "mappedField")
    public String getMappedField() {
        return mappedField;
    }

    public void setMappedField(String mappedField) {
        this.mappedField = mappedField;
    }

}

详细信息


答案 2

我不明白为什么这个JAXB非法注释异常被抛出

我也得到了例外,这似乎是由于我的Spring布线中的依赖层次结构不正确。### counts of IllegalAnnotationExceptions

我通过在 JAXB 代码中放置一个断点来解决这个问题。对我来说,这是在.然后我转储了变量,它给出了如下结果:com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check()list

[org.mortbay.jetty.Handler is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
    at org.mortbay.jetty.Handler
    at public org.mortbay.jetty.Handler[] org.mortbay.jetty.handler.HandlerCollection.getHandlers()
    at org.mortbay.jetty.handler.HandlerCollection
    at org.mortbay.jetty.handler.ContextHandlerCollection
    at com.mprew.ec2.commons.server.LocalContextHandlerCollection
    at private com.mprew.ec2.commons.server.LocalContextHandlerCollection com.mprew.ec2.commons.services.jaxws_asm.SetLocalContextHandlerCollection.arg0
    at com.mprew.ec2.commons.services.jaxws_asm.SetLocalContextHandlerCollection,
org.mortbay.jetty.Handler does not have a no-arg default constructor.]
....

在我看来,这似乎具有误导性。也许我不明白例外在说什么。但它确实表明我的.我删除了一个依赖项循环,并清除了错误。does not have a no-arg default constructorLocalContextHandlerCollection

希望这对其他人有所帮助。


推荐