JAXB,setter / getter的注释 [英] JAXB, annotations for setter/getter

查看:96
本文介绍了JAXB,setter / getter的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@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>();
     }  
}  

当我在Java代码中创建User对象时创建XML,然后通常都是。

但是当我尝试从XML获取User-object时,字段 values 总是 null 。所以setter不在这里工作。可能setter也需要一些注释吗?

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 values always null. So setter not working here. May be setter needs some annotation too?

XML看起来像

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


推荐答案

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

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

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;
    }

}

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

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

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>();
    }

}

这篇关于JAXB,setter / getter的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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