简体   繁体   中英

Java Component in Mule does not use project's UTF-8 encoding

I have a java component in Mule that is returning a String. When I print that string from within the java component, it reads as UTF-8 just fine in the console. However, when I return this as the payload, the subsequent logger and all downstream components can no longer read the UTF-8 characters. I've confirmed that my project's encoding setting is set to 'UTF-8'. I've also:

  1. Setting MULE_ENCODING=UTF-8 before and after the Java component, both as an outbound property and invocation property.
  2. Setting the encoding on the message in a mule expression, but that failed message.setEncoding('UTF-8') with an error stating it could not find the method setEncoding()
  3. Using the following in the java component:

     eventContext.getMessage().setPayload(myString); eventContext.getMessage().setEncoding("UTF-8"); return eventContext.getMessage().getPayload(); 
  4. Switching to a java transformer instead of a component, using the below config on the transformer, explicitly setting encoding to "UTF-8".

     <custom-transformer encoding="UTF-8" class="myDomain.myJavaTransformer" doc:name="Java"/> 

I'm not sure what else to try to ensure the payload coming out of the java component/transformer is not interpreted or converted to another encoding...

The output should read: “Outstanding New Environmental Scientist”

The logger currently outputs is “Outstanding New Environmental Scientistâ€. I've corrected this in other parts of the flow by adding encoding=UTF-8 to any VM endpoints, for example. I've also confirmed that this can print correctly in a logger elsewhere in the flow.

There seems to be something happening after the String is returned from within the java class referenced by the java component and the subsequent mule logger...

在您的Java组件之后尝试<object-to-string-transformer encoding="..." />

I had a problem with encoding too. It was not completely similar to your case but I took this action and it worked for me. (Byte to array using java serialization)

<set-property propertyName="MULE_ENCODING" value="UTF-8" doc:name="Property" />

<byte-array-to-string-transformer encoding="UTF-8" doc:name="Byte Array to String"/>

I used that when message is coming to my flow.

Also there is properties in wrapper and you can set that as well.

How are you reading the RSS feed? If it is using an endpoint, try setting the 'encoding' attribute for that endpoint?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM