问题描述
我在 Spring 上下文中使用 XStream
时遇到问题.
方法 ctxXStream.addImplicitCollection(Class ownerType, String fieldName, String itemFieldName, Class itemType)
如果我们在 pom 中导入 xstream 依赖项,则它无法正常工作,但如果我们导入 xstream,它确实可以工作源代码,我们在 pom 中排除了相关的依赖.
The method c.t.x.XStream.addImplicitCollection(Class ownerType, String fieldName, String itemFieldName, Class itemType)
doesn't work correctly if we import the xstream dependency in pom but it does work if we import xstream source code and we esclude related dependency in pom.
我们使用了以下依赖项的版本:
We used the following dependencies' version:
- xstream:1.4.11.1
- spring-boot-dependencies:2.1.2.RELEASE
使用 pom.xml
中的 xstream 依赖项测试 fromXml
我们有以下错误:
Testing fromXml
with the xstream's dependency in the pom.xml
we have the following error:
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
它能正常工作.
Testing fromXml
with the xstream's source code it does work correctly.
在 Spring 上下文中使用 JUnit
测试 fromXml
确实可以正常工作.
Testing fromXml
with JUnit
in Spring context it does work correctly.
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>
你有什么想法吗?
Do you have any ideas?
推荐答案
我们解决了这个问题.
从 pom 的依赖项中删除 spring-boot-devtools
它确实可以正常工作.
Removing spring-boot-devtools
from pom's dependencies it does work correctly.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
XStream 的依赖项不在类加载器中.我们在 META-INF
中添加了一个包含 xstream 异常规则的异常文件,然后我们再次添加了 spring-boot-devtools
的依赖项.
The XStream's dependency was not in classloader. We added an exception file in META-INF
that contains an exception rule for xstream then we added again the spring-boot-devtools
's dependency.
文件 spring-devtools.properties 包含以下规则:
The file spring-devtools.properties containts the following rule:
restart.include.example-shared=/xstream[\\.\\w-]+\.jar
这篇关于在 Spring 上下文中使用 XStream 时出错:DuplicateFieldException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!