首页 > 解决方案 > 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 找不到或没有用于隐式集合的映射器。这看起来应该很简单,我已经尝试按照我找到的所有教程进行操作。然而总是这个错误。

标签: javaxmlspring-bootdeserializationxstream

解决方案


好吧...我没有做错任何事。Spring Boot Dev Tools 是如何破坏它的。 在 Spring 上下文中使用 XStream 时出错:DuplicateFieldException

类似的问题不同的例外(尽管这两个例外都是不相关且具有误导性的)。


推荐阅读