使用 XStreamMarshaller 的多个 setImplicitCollections

Multiple setImplicitCollections using XStreamMarshaller

我正在尝试 XStreamMarshaller。但是当我尝试解析两个 xsd:complexType of xml 文件时,我收到了这个错误:

class[1]            : com.mc.batch.mapping.authorization.PIECES_JOINTES
converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter

Xml :

<DOCUMENT>

    <ARTICLES>
        <ARTICLE>
            <NUMERO_ARTICLE>1</NUMERO_ARTICLE>
        </ARTICLE>
        <ARTICLE>
            <NUMERO_ARTICLE>2</NUMERO_ARTICLE>
        </ARTICLE>
    </ARTICLES>

    <PIECES_JOINTES>
        <PIECES_JOINTE>
            <TYPE_DOCUMENT>PDF</TYPE_DOCUMENT>
        </PIECES_JOINTE>
        <PIECES_JOINTE>
            <TYPE_DOCUMENT>WORD</TYPE_DOCUMENT>
        </PIECES_JOINTE>
        <PIECES_JOINTE>
            <TYPE_DOCUMENT>XLS</TYPE_DOCUMENT>
        </PIECES_JOINTE>
    </PIECES_JOINTES>

</DOCUMENT>

代码:

@Bean
MessageConverter messageConverter() {

    Map<String, Class<?>> aliases = new HashMap<>();

    XStreamMarshaller marshallerAuthorization = new XStreamMarshaller();
    aliases.put("DOCUMENT", DOCUMENT.class);
    marshallerAuthorization.setAliases(aliases);

    Map implicitArticle = Collections.singletonMap(ARTICLES.class, "ARTICLE");
    Map implicitPiece   = Collections.singletonMap(PIECES_JOINTES.class, "PIECES_JOINTE");

    marshallerAuthorization.setImplicitCollections(implicitPiece);
    marshallerAuthorization.setImplicitCollections(implicitArticle);

    MarshallingMessageConverter messageConverterAuthorization = new MarshallingMessageConverter(marshallerAuthorization);
    messageConverterAuthorization.setTargetType(MessageType.TEXT);
    return messageConverterAuthorization;
}

但是如何使用两个setImplicitCollections来映射PIECES_JOINTES.classARTICLES.class

如何解决此冲突?欢迎任何帮助。提前致谢。

how to use two setImplicitCollections for mapping PIECES_JOINTES.class and ARTICLES.class

您不需要调用 setImplicitCollections 两次,第二次调用中传递的值将覆盖第一次调用。此方法接受地图,因此您可以这样写:

Map<Class<?>, String> implicitCollections = new HashMap<>();
implicitCollections.put(ARTICLES.class, "ARTICLE");
implicitCollections.put(PIECES_JOINTES.class, "PIECES_JOINTE");

marshallerAuthorization.setImplicitCollections(implicitCollections);

而不是:

Map implicitArticle = Collections.singletonMap(ARTICLES.class, "ARTICLE");
Map implicitPiece   = Collections.singletonMap(PIECES_JOINTES.class, "PIECES_JOINTE");

marshallerAuthorization.setImplicitCollections(implicitPiece);
marshallerAuthorization.setImplicitCollections(implicitArticle);