Search code examples
jaxbmuleanypoint-studio

JAXB messing up encoding in Mule flow


I'm running a flow in Mule CE and have huge problems with encodings. No matter what I do my files end up with messed up non-english charcters.

Before the jaxb-object-to-xml transformer my payload looks nice in the console and in the debugger, but after that it's all messed up.

    ......   
    <http:request>
    <object-to-byte-array-transformer encoding="UTF-8" doc:name="Object to Byte Array"/>
    <object-to-string-transformer doc:name="String" encoding="UTF-8"/> 
    <json:json-to-object-transformer returnClass="java.util.List"  doc:name="JSON2ObjectList" encoding="UTF-8"/>
    <collection-splitter doc:name="Collection Splitter"/>
    <choice doc:name="Choice">
       <when expression="....">
        <custom-transformer returnClass="se.system.Order.SalesHeader"  class="se.system.Transformer.Map2Order" doc:name="Map2Order" mimeType="application/xml" encoding="UTF-8"/>
        <mulexml:jaxb-object-to-xml-transformer name="orderMarshaller" jaxbContext-ref="JAXB_Context" doc:name="orderMarshaller"  mimeType="text/xml" encoding="UTF-8"/>
    <object-to-string-transformer doc:name="XML2String" encoding="UTF-8"/>
    <set-variable variableName="fileName" value="order-#[function:dateStamp].xml" doc:name="fileName" encoding="UTF-8"/>       
    <file:outbound-endpoint path="${file.ToOrder}" responseTimeout="10000" doc:name="File" outputPattern="#[fileName]" mimeType="text/xml" encoding="UTF-8"/>

After the jaxb transformer non-english characters looks like:

Deliveryinfo2="å ä ö Å Ä Ö &amp; % è É"/

And the 010 editor claims its ANSI DOS (with messed up characters, don't know if that one is to be trusted though) Have I missed something in the jaxb transformer? or somewhere else? Is it possible to replace it with a Java component, initiate my very own JAXB context, get a marshaller and handle it myself? No clues anymore...

Regards

EDIT: this one can handle non-english characters

<mulexml:object-to-xml-transformer doc:name="Object to XML" encoding="UTF-8" />

but not GregorianCalendar types or my main Objects List of other objects so it's not an alternative


Solution

  • This seems to be a bug caused by the JAXB transformer not respecting the given encoding, see source (line 64).

    What however is kinda weird is that according to the JAXB documentation the default encoding should be UTF-8.

    Encoding

    By default, the Marshaller will use UTF-8 encoding when generating XML data to a java.io.OutputStream, or a java.io.Writer. Use the setProperty API to change the output encoding used during these marshal operations. Client applications are expected to supply a valid character encoding name as defined in the W3C XML 1.0 Recommendation and supported by your Java Platform.

    This should probably be something like this

    final Marshaller m = jaxbContext.createMarshaller();
    m.setProperty(Marshaller.JAXB_ENCODING, encoding);