gpt4 book ai didi

java - 通用列表上的 XStream 异常

转载 作者:行者123 更新时间:2023-11-29 05:41:37 26 4
gpt4 key购买 nike

使用 XStream 1.4.4。

我有以下 XML:

<app name="MyApp">
<properties>
<property name="fizz" value="buzz" />
<property name="foo" value="bar" />
</properties>

<fruit type="apple" />

<!-- ...etc. -->
</app>

以及属性列表的相应 POJO,以及属性本身:

@XStreamAlias("properties")
public class Properties {
private List<Property> properties = new ArrayList<Property>();

public List<Property> getProperties() {
return properties;
}

public void setProperties(List<Property> properties) {
this.properties = properties;
}
}

@XStreamAlias("property")
public class Property {
private String name = null;

private String value = null;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}

当我尝试运行以下代码时:

XStream xs = new XStream();
Strnig xml = getXML(); // Fetches the string of XML from above
App app = (App)xs.fromXML(xml);

我得到:

Exception in thread "main" com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field com.me.myapp.Properties.property
---- Debugging information ----
field : property
class : com.me.myapp.Properties
required-type : com.me.myapp.Properties
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path : /app/properties/property
line number : 4
class[1] : com.me.myapp.App
version : null
-------------------------------
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.determineType(AbstractReflectionConverter.java:453)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:294)
...rest of stack trace omitted for brevity

我哪里错了?

最佳答案

您根本不需要注释。只需使用别名 Property.class 作为“属性”,XStream 将为您处理所有其他事情。

关于java - 通用列表上的 XStream 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17348853/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com