Search code examples
springspring-integrationspring-batchxstream

Multiple setImplicitCollections using XStreamMarshaller


I am trying XStreamMarshaller. But when I try to parse two xsd:complexType of xml file i am getting this error :

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>

code :

@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;
}

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

How do I resolve this conflict ? Any help would be welcome. Thanks in advance.


Solution

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

    You don't need to call setImplicitCollections twice, the value passed in the second call will override the first one. This method accepts a map, so you can write something like:

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

    Instead of:

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