java - XStream 反序列化简单 XML 在应该创建集合时不断给出错误说没有这样的字段
问题描述
试图将以下 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 是如何破坏它的。 在 Spring 上下文中使用 XStream 时出错:DuplicateFieldException
类似的问题不同的例外(尽管这两个例外都是不相关且具有误导性的)。
推荐阅读
- javascript - 单击其他时停止播放音频,多次单击时没有重复事件
- javascript - 使用-> document.elemetbyid 在 javascript 的 givinen 输入中字母计数不重复
- php - 单击表中的特定行时如何查看数据库中特定行的数据
- c++ - OpenGL中的对数深度缓冲区与glad / glfw
- reactjs - React Native TypeError:无法读取未定义的属性'backgroundVideo'
- string - 拆分十六进制字符串:返回最小拆分数,使得每个拆分都是完美正方形的十六进制表示
- javascript - 如何允许在整个 JavaScript 文件正文中递增的变量在页面重新加载后保留其新值
- entity-framework-core - 现有 Db,无效的列名
- c# - WPF UserControl 运行时错误:无法在 UserControl 中找到图标的路径
- r - 在 R 中暂停和恢复插入符号训练