I can't understand why this JAXB IllegalAnnotationException is thrown

后端 未结 12 1763
渐次进展
渐次进展 2020-12-13 02:03

This is my XML file:


    
    
    
                   


        
12条回答
  •  悲&欢浪女
    2020-12-13 02:36

    This is because, by default, Jaxb when serializes a pojo, looks for the annotations over the public members(getters or setters) of the properties. But, you are providing annotations on fields. so, either change and set the annotations on setters or getters of properties, or sets the XmlAccessortype to field.

    Option 1::

    @XmlRootElement(name = "fields")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Fields {
    
            @XmlElement(name = "field")
            List fields = new ArrayList();
            //getter, setter
    }
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Field {
    
           @XmlAttribute(name = "mappedField")
           String mappedField;
           //getter,setter
    }
    

    Option 2::

    @XmlRootElement(name = "fields")
    public class Fields {
    
            List fields = new ArrayList();
    
            @XmlElement(name = "field")
            public List getFields() {
    
            }
    
            //setter
    }
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Field {
    
           String mappedField;
    
           @XmlAttribute(name = "mappedField")
           public String getMappedField() {
    
           }
    
            //setter
    }
    

    For more detail and depth, check the following JDK documentation http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlAccessorType.html

提交回复
热议问题