Java 放在 getter 与 setter 与成员上的 JAXB 注释之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22196082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:15:44  来源:igfitidea点击:

What's the difference between JAXB annotations put on getter versus setters versus members?

javaxmljaxbmarshallingunmarshalling

提问by stewenson

Title says it all.

标题说明了一切。

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.

我想知道将 JAXB 注释(如@XmlElement)放在 field/getter/setter 上的主要区别是什么。在我看来(在简单的情况下)这无关紧要。

E.g. lets take this

例如让我们把这个

class A  {
    private String a;

    public String getA() { return a; }

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

now it seems to me that it does not matter if I put @XmlElementon 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?

现在在我看来,我是放在@XmlElement成员字段上还是放在 getter/setter 上都没有关系。它只是编组确定。当我需要有所作为并且何时重要时,是否有任何用例?

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

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

I am using JAXB MOXy implementation

我正在使用 JAXB MOXy 实现

Thanks

谢谢

采纳答案by bdoughan

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.

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

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

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

For More Information

想要查询更多的信息

回答by Rahul Khimasia

I have found no differences in marking JAXB annotations on setter methods and getter methods. I tested both marshalling and unmarshalling, and they both worked fine. But you should only annotate one of them; either the getter method or the setter method; you cannot annotate both, else you will get a runtime exception like the following.

我发现在 setter 方法和 getter 方法上标记 JAXB 注释没有区别。我测试了编组和解组,它们都运行良好。但是你应该只注释其中一个;getter 方法或 setter 方法;您不能同时注释两者,否则您将收到如下所示的运行时异常。

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:445)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:462)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)