JAXB:注解@XmlAccessorType 是否仅用于序列化而与数据绑定无关? [英] JAXB : Is the annotation @XmlAccessorType is only for Serialization and nothing to do with Binding of data?
问题描述
我想知道为什么我们在使用 JAXB 时需要指定 Annotation @XmlAccessorType.
I wanted to know why do we need to specify the Annotation @XmlAccessorType when working with JAXB .
当我用谷歌搜索这个时,我从一个网站上找到了这个描述@XmlAccessorType 设置默认字段和属性可序列化.默认情况下,JAXB 序列化公共字段和属性.通过设置@XmlAccessorType,bean 可以选择只允许被注释的字段被序列化.
When i googled for this i found out this description from a website stating this @XmlAccessorType sets default field and property serializability. By default, JAXB serializes public fields and properties. By setting @XmlAccessorType, the bean can choose to only allow annotated fields to be serialized.
这里作者提到,通过这个注解,它可以控制序列化.
Here the author mentions that with this annotation it gives control on serialization .
我的问题是,所以@XmlAccessorType 与从 XML 到 java 和从 java 到 XML 的 JAXB 绑定和解除绑定无关,它只是关于序列化.
My question is , so @XmlAccessorType has nothing to do with the JAXB Binding and Unbinding from XML to java and java to XML , and it is all about Serialization only .
推荐答案
JAXB 的 @XmlAccessorType
注解仅供 JAXB (JSR-222) 用于确定如何将文件编组到/从 XML 中的实现:
JAXB's @XmlAccessorType
annotation is only used by JAXB (JSR-222) implementations for determining how to marshal a file to/from XML:
通常要做出的主要决定是在 FIELD
和属性
/公共
.FIELD
在您的 get/set 方法中有您不想在编组/解组期间触发的逻辑时特别有用.要查看此选择影响映射元数据的一种方式,请参阅:
Normally the main decision to be made is between FIELD
& PROPERTY
/PUBLIC
. FIELD
is particularly useful when you have logic in your get/set methods that you do not want triggered during marshalling/unmarshalling. To see one way this choice affects the mapping metadata see:
NONE
当您有许多未映射的属性并且您想告诉您的 JAXB 实现仅映射您已注释的字段/属性时,这是一个有用的选择.这比在模型中添加大量 @XmlTransient
注释要容易得多.
NONE
is a useful choice when you have many unmapped properties and you want to tell your JAXB implementation to only map the fields/properties you have annotated. This can be alot easier than adding a lot of @XmlTransient
annotations into your model.
更多信息
这篇关于JAXB:注解@XmlAccessorType 是否仅用于序列化而与数据绑定无关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!