JAXB不调用Setter方法 [英] JAXB does not call Setter method

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

问题描述

我无法理解我做错了什么。
我想使用JAXB解组xml,但我注意到没有调用setter方法。
我使用的是Java 1.5。
Attribute.java类中的Getter和Setter - 工作正常,但在Configuration.java类中 - Setter方法不调用。你能告诉我我哪里错了吗?

I cannot understand what am I doing wrong. I want to unmarshall an xml using JAXB, but i noticed that setter method wasn't called. I am using Java 1.5. Getters and Setters in Attribute.java class - work correctly, but in Configuration.java class - Setter method doesn't call. Can you please show me where am I wrong?

@XmlRootElement(name="configuration")
@XmlAccessorType(XmlAccessType.NONE)
public class Configuration {    
    public List< Configuration> getItems() {
        return new ArrayList<Attribute>(getMap().values());
    }

    @XmlElement(name="attributes")
    public void setItems(List<Attribute> attributes) {
        getMap().clear();
        for (Attribute attribute : attributes) {
            getMap().put(attribute.getName(), attribute);
        }
    }

    private Map<String, Attribute> map;

    public Map<String, Attribute> getMap() {

        if (map == null) {
            map = new HashMap<String, Attribute>();
        }
        return map;
    }
}

我的XML看起来像这样:

My XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <attributes name="some_name" type="calculation" value="select ? from dual" priority="0"/>
</configuration>


推荐答案

如果列表从getter返回,JAXB impl将使用它来添加集合项而不是创建新项目并通过setter设置它。

If a List is returned from the getter a JAXB impl will use that to add the collection items to instead of creating a new one and setting it via the setter.

这样做的目的是让您有机会初始化最适合您的域模型的 List 的实现。

The purpose of this is to give you the chance to initialize the implementation of List that best fits your domain model.

这篇关于JAXB不调用Setter方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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