我无法理解我做错了什么.我想使用JAXB解组xml,但我注意到没有调用setter方法.我使用的是Java 1.5.Attribute.java类中的Getters和Setter - 工作正常,但在Configuration.java类中 - Setter方法不调用.你能告诉我我哪里错了吗?
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
我的XML看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<attributes name="some_name" type="calculation" value="select ? from dual" priority="0"/>
</configuration>
Run Code Online (Sandbox Code Playgroud)
如果List从getter返回a,则JAXB impl将使用它来添加集合项而不是创建新集合并通过setter设置它.
这样做的目的是让您有机会初始化List最适合您的域模型的实现.
| 归档时间: |
|
| 查看次数: |
1851 次 |
| 最近记录: |