java JAXB,setter/getter 的注解

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10617267/
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-10-31 01:50:50  来源:igfitidea点击:

JAXB, annotations for setter/getter

javajaxb

提问by Ilya

@XmlType  
@XmlAccessorType(XmlAccessType.FIELD)  // here I need this access
public class User implements Serializable 
{  
     // ...  

     @XmlTransient
     private Set<Values> values;

     // ...

     @XmlElement
     private Set<History> getXmlHistory()
     {
         return new CustomSet<Values, History>(Values);
     }

     private void setXmlHistory(final Set<History> aHistory)
     {
         this.values = new HashSet<Values>();
     }  
}  

When I am create User object in Java code and after create XML, then all normally.
But when I try to get User-object from XML, then field valuesalways null. So setter not working here. May be setter needs some annotation too?

当我在 Java 代码中创建 User 对象并在创建 XML 之后,一切正常。
但是当我尝试从 XML 获取用户对象时,字段values总是null. 所以二传手不在这里工作。可能是 setter 也需要一些注释吗?

XML looks like

XML 看起来像

<user>  
   ...  
      <xmlHistory>  
       // ... record 1 
      </xmlHistory>  
      <xmlHistory>  
      // ... record 2 
      </xmlHistory>  
</user>  

回答by bdoughan

I do not believe that this is a JAXB problem, as the following model will work:

我不相信这是 JAXB 问题,因为以下模型将起作用:

package forum10617267;

import java.io.Serializable;
import java.util.*;
import javax.xml.bind.annotation.*;

@XmlType
@XmlAccessorType(XmlAccessType.FIELD) // here I need this access
public class User implements Serializable {

    @XmlTransient
    private Set<History> history = new HashSet<History>();

    @XmlElement
    private Set<History> getXmlHistory() {
         return history;
    }

    private void setXmlHistory(final Set<History> aHistory) {
        this.history = aHistory;
    }

}

The problem you are seeing is a result of the logic you have in your get/set methods. Since your valuesfield is not initialized, I am not sure how CustomSetwould be able to update it.

您看到的问题是您在 get/set 方法中的逻辑的结果。由于您的values字段未初始化,我不确定如何CustomSet更新它。

package forum10617267;

import java.io.Serializable;
import java.util.*;
import javax.xml.bind.annotation.*;

@XmlType
@XmlAccessorType(XmlAccessType.FIELD) // here I need this access
public class User implements Serializable {

    @XmlTransient
    private Set<Values> values;

    @XmlElement
    private Set<History> getXmlHistory() {
         return new CustomSet<Values, History>(values);
    }

    private void setXmlHistory(final Set<History> aHistory) {
        this.values = new HashSet<Values>();
    }

}

回答by alegen

I believe that @XmlAccessorType(XmlAccessType.FIELD)in combination with your @XmlTransientis the source of the problem. Have you tried without the transient annotation?

我相信,@XmlAccessorType(XmlAccessType.FIELD)与您的结合@XmlTransient是问题的根源。您是否尝试过不使用瞬态注释?