getter与setter与成员之间的JAXB注释之间的区别是什么?

问题描述:

标题说明了一切。

我想知道放置JAXB注释之间的主要区别(如 @XmlElement )在field / getter / setter上。在我看来(在简单的情况下)并不重要。

I would like to know what is the principial difference between putting JAXB annotation (like @XmlElement) on field / getter / setter. It seems to me that (in simple cases) it does not matter.

例如。让我们拿这个

class A  {
    private String a;

    public String getA() { return a; }

    public void setA(String a) { this.a = a; }
}

现在在我看来,如果我把 @XmlElement 。它只是编组好的。当我需要做出改变并且它确实重要时,是否有任何用例?

now it seems to me that it does not matter if I put @XmlElement on member field or on getter / setter. It just marshalls ok. Are there any usecases when I need to make difference and when it does matter?

当我去解组这个(xml回到A)JAXB具体做什么?

When I go to unmarshall this (xml back to A) what JAXB does specifically?

我正在使用JAXB MOXy实现

I am using JAXB MOXy implementation

谢谢

默认情况下,JAXB impls会将属性(获取/设置对),公共字段(实例变量)和带注释的非公共字段视为映射。如果您只是注释一个字段,您将获得重复的映射属性异常。

By default JAXB impls will treat properties (get/set pairs), public fields (instance variables), and annotated non-public fields as mapped. If you just annotate a field you will get a duplicate mapped property exception.

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

If you want to annotate the field you should specify @XmlAccessorType(XmlAccessType.FIELD) on the class.

更多信息

  • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html