getter与setter与成员之间的JAXB注释之间的区别是什么? [英] What's the difference between JAXB annotations put on getter versus setters versus members?

查看:82
本文介绍了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; }
}

现在在我看来,如果我把<$无所谓成员字段或getter / setter上的c $ c> @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

这篇关于getter与setter与成员之间的JAXB注释之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆