java - 在 Spring 上下文 : DuplicateFieldException 中使用 XStream 时出错

标签 java xml spring spring-boot xstream


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

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

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

  • xstream:1.4.11.1
  • spring-boot-dependencies:2.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>

你有什么想法吗?

最佳答案

我们解决了这个问题。

从 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

关于java - 在 Spring 上下文 : DuplicateFieldException 中使用 XStream 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59176413/

相关文章:

c# - 如何使用采用 XDocument 的构造函数将 XML 反序列化为对象?

java - Spring Boot Controller 不返回 html 页面

spring - Spring Data Rest 2.1 中的子资源分页

java - boolean 字段的 JSON Post 请求默认发送 false

java - 在android中使用简单的java类进行Facebook native 登录

java - 滑动过渡可返回,但按后退按钮不会返回

spring - 正确使用 Spring 环境配置文件以管理 PropertySourcesPlaceholderConfigurer 和属性文件集

java - 将 servlet 移植到 Web 服务 - 访问上下文?

xml - JIBX:如何使用多个目标命名空间?

java - 我应该从layout_weight转向dip吗?