Error using XStream in Spring context: DuplicateFieldException

心不动则不痛 提交于 2021-02-19 04:28:12

问题



I've a problem using XStream in Spring context.

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

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)

Testing fromXml with the xstream's source code it does work correctly.

Testing fromXml with JUnit in Spring context it does work correctly.

Example of 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?


回答1:


We solved the problem.

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>

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.

The file spring-devtools.properties containts the following rule:

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


来源:https://stackoverflow.com/questions/59176413/error-using-xstream-in-spring-context-duplicatefieldexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!