【问题标题】:Error using XStream in Spring context: DuplicateFieldException在 Spring 上下文中使用 XStream 时出错:DuplicateFieldException
【发布时间】:2020-03-29 06:41:54
【问题描述】:


我在 Spring 上下文中使用 XStream 时遇到问题。

如果我们在 pom 中导入 xstream 依赖项,c.t.x.XStream.addImplicitCollection(Class ownerType, String fieldName, String itemFieldName, Class itemType) 方法将无法正常工作,但如果我们导入 xstream 源代码并在 pom 中排除相关依赖项,则它确实有效。

我们使用了以下依赖项的版本:

  • xstream1.4.11.1
  • spring-boot-dependencies2.1.2.RELEASE

pom.xml 中使用xstream 的依赖项测试fromXml 我们有以下错误:

com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$DuplicateFieldException: Duplicate field point
---- Debugging information ----
message             : Duplicate field point
field               : point
class               : c.g.m.r.d.Child
required-type       : c.g.m.r.d.Child
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /root/child/point[2]
line number         : 1
class[1]            : c.g.m.r.d.Root
required-type[1]    : c.g.m.r.d.Root
version             : 1.4.11.1
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$3.add(AbstractReflectionConverter.java:287)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:457)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:277)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:499)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:425)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:277)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1487)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1467)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1338)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1329)

使用 xstream 的源代码测试 fromXml 确实可以正常工作。

在 Spring 上下文中使用 JUnit 测试 fromXml 确实可以正常工作。

XML 示例:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <child>
        <point label="...">
            <date>...</date>
            <value>...</value>
        </point>
        <point label="...">
            <date>...</date>
            <value>...</value>
        </point>
    </child>
</root>

你有什么想法吗?

【问题讨论】:

    标签: java xml spring spring-boot xstream


    【解决方案1】:

    我们解决了这个问题。

    从 pom 的依赖项中删除 spring-boot-devtools 它确实可以正常工作。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
    

    XStream 的依赖项不在类加载器中。我们在 META-INF 中添加了一个异常文件,其中包含 xstream 的异常规则,然后我们再次添加了 spring-boot-devtools 的依赖项。

    文件spring-devtools.properties包含以下规则:

    restart.include.example-shared=/xstream[\\.\\w-]+\.jar
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 2014-12-20
      • 2013-07-22
      • 2020-07-05
      • 1970-01-01
      • 2015-12-22
      • 2019-08-12
      相关资源
      最近更新 更多