JAXB:注释@XmlAccessorType是仅用于序列化而与数据绑定无关? [英] JAXB : Is the annotation @XmlAccessorType is only for Serialization and nothing to do with Binding of data?

查看:106
本文介绍了JAXB:注释@XmlAccessorType是仅用于序列化而与数据绑定无关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么在使用JAXB时我们需要指定注释@XmlAccessorType。



当我用Google搜索时,我从网站上找到了这个描述声明这个
@XmlAccessorType设置默认字段和属性可序列化。默认情况下,JAXB序列化公共字段和属性。通过设置@XmlAccessorType,bean可以选择仅允许对带注释的字段进行序列化。



这里作者提到通过这个注释它可以控制序列化。



我的问题是,所以@XmlAccessorType与从XML到java和java到XML的JAXB绑定和解除绑定无关,而且它只是关于序列化的。 / p>

解决方案

JAXB的 @XmlAccessorType 注释仅由JAXB(JSR-222) 用于确定如何将文件封送到XML或从XML封送文件的实现: / p>

通常,主要决定是 FIELD & PROPERTY / PUBLIC FIELD 在您在get / set方法中具有您不希望在编组/解组时触发的逻辑时特别有用。要查看此选项影响映射元数据的一种方式,请参阅:





NONE 是一个有用的选择,如果你有许多未映射的属性,并且你想告诉你的JAXB实现只映射您已注释的字段/属性。这比在模型中添加大量 @XmlTransient 注释要容易得多。



Fore More信息




I wanted to know why do we need to specify the Annotation @XmlAccessorType when working with JAXB .

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 .

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's @XmlAccessorType annotation is only used by JAXB (JSR-222) implementations for determining how to marshal a file to/from XML:

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 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.

Fore More Information

这篇关于JAXB:注释@XmlAccessorType是仅用于序列化而与数据绑定无关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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