默认情况下, JAXB(JSR-222)
实现将公共属性(获取/设置方法)和带注释的字段视为已映射(和分开)。默认映射是
@XmlElement这样,因此您的属性将被视为以此方式映射。
解决方案1
由于您要注释字段,因此需要
@XmlAccessorType(XmlAccessType.FIELD)在类中添加。
@XmlAccessorType(XmlAccessType.FIELD)public class Parameter { @XmlAttribute(name = "attr") private String mName; @XmlValue private String mValue; public String getName() { return mName; } public void setName(String aName) { this.mName = aName; } public String getValue() { return mValue; } public void setValue(String aValue) { this.mValue = aValue; }}解决方案#2
注释get(或set)方法。
public class Parameter { private String mName; private String mValue; @XmlAttribute(name = "attr") public String getName() { return mName; } public void setName(String aName) { this.mName = aName; } @XmlValue public String getValue() { return mValue; } public void setValue(String aValue) { this.mValue = aValue; }}想要查询更多的信息
- http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
- http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html
更新
您还需要
@XmlElement在
mappings属性上使用注释,以指定元素名称应为
mapping。
@XmlRootElement(name = "mappings")public class Mappings { private List<Mapping> mMappings; @XmlElement(name="mapping") public List<Mapping> getMappings() { return mMappings; } public void setMappings(List<Mapping> aMappings) { this.mMappings = aMappings; }}


