问题描述
正在尝试将以下 xml 转换为 java 对象...
<?xml version="1.0" encoding="utf-8"?>
<layertypes>
<layertype id="layer_1" label="first" />
<layertype id="layer_2" label="Second" />
</layertypes>
使用这些类:
@XStreamAlias("layertype")
public class LayerType {
@XStreamAsAttribute
private String id;
@XStreamAsAttribute
private String label;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
和
@XStreamAlias("layertypes")
public class LayerTypes {
@XStreamImplicit(itemFieldName = "layertype")
private List<LayerType> layertypes = new ArrayList<>();
public List<LayerType> getLayertypes() {
return layertypes;
}
public void setLayertypes(List<LayerType> layertypes) {
this.layertypes = layertypes;
}
}
从这里调用这些类和 xstream:
XStream xstream = new XStream();
xstream.processAnnotations(LayerType.class);
xstream.processAnnotations(LayerTypes.class);
LayerTypes layerTypes = (LayerTypes) xstream.fromXML(inputStream);
我明白了:
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnkNownFieldException: No such field xbeans.LayerTypes.layertype
---- Debugging information ----
message : No such field xbeans.LayerTypes.layertype
field : layertype
class : xbeans.LayerTypes
required-type : xbeans.LayerTypes
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path : /layertypes/layertype
line number : 3
version : 1.4.15
-------------------------------
知道我做错了什么吗?单步调试调试器就像 Xstream 找不到或没有隐式集合的映射器。这似乎应该很简单,我已经尝试按照我找到的所有教程进行操作。然而总是这个错误。
解决方法
好吧……我没有做错任何事。 Spring Boot Dev Tools 是如何破坏它的。 Error using XStream in Spring context: DuplicateFieldException
类似的问题不同的例外(尽管这两个例外都无关紧要且具有误导性)。