How to solve Chinese garbled characters generated by mule

Source: Internet
Author: User

In release Web Services
Time , WSDL
As a service description, there is a label in it that can be written Chinese
Is used as an annotation to write descriptive information. This label is <WSDL: Documentation>
. Use the Open Source ESB bus in the oecp Platform Mule
Do it WebService
Used for publishing. encountered during Publishing Chinese
Description Garbled
Problem: Read mule and cxf's Source code The problem is finally solved. Here we record the solution process and method.
Mule
To convert a Java methodWebService
. Cxf has an annotation generated WSDL
In <WSDL: Documentation>
Tag. This annotation is @ Wsdldocumentation
. But after this label is used, the generated WSDL
File, Chinese
Is displayed Garbled
.
Tests show that when spring-cxf is used, @ Wsdldocumentation
No Garbled
. This indicates the problem. Mule
. Only one review is allowed. Mule
Of
Source Code To find the cause. Because the source code cannot be found to be the same as the current jar package version, only the source code of the upgraded version can be used. During debugging, it is found that the upgrade changes are relatively large, and the previous methods and
The Code has been modified quite a lot, and missing lines in the wrong line can lead to poor readability of debugging. I have been reading it for more than a day. It is comforting to find the cause of the problem and solve it. The following describes the principles. Freight
We know the release WebService
Later, use URL +? The service description file will be obtained when you access the service in WSDL mode. WSDL
File. Mule
After publishing the service,

Mark ①, which is caused by an HTTP requestMule
Event cascade chain, yesMule
Internal Architecture mechanism. This is not what we need to pay attention to now. The focus is on the second row. The outputstream marked in red is used to carryWSDL
Object of the file stream, which is actually a bytearrayoutputstream. After the execution is completed at the position 6th in the figure, the stream will be written completely. Previous stepsChinese characters
The encoding is correct. Even if it is written to the stream, the character Byte encoding is no problem. However, when we view the content of this stream, we can seeGarbled
. Why? Why is the byte stored in stream correct but garbled?
Frequent debuggingProgramWhen we view the content of an object, the object viewer calls the tostring method of the object. After checking, we found that the tostring method of bytearrayoutputstream was overwritten:

Java code girls cut their hair for the first time
    1. Public
       
      Synchronized
      String tostring (){
    2. Return
       
      New
      String (BUF,
      0
      , Count );
    3. }


Bytearrayoutputstream uses all the bytes in the buffer to create and return a String object. Note that the string is not created
The specified encoding format is available. So what encoding format does it use? Let's look at the string. String uses stringcoding to convert byte to a char array, and its
The selected encoding format is obtained from charset. defaultcharset. We can find this method from the jdk api, as mentioned above:

Default value obtained in our server running environment Encoding
The format is GBK. The reason for garbled characters is found: the service generated by spring-cxf WSDL
The UTF-8 encoding is used, and Mule
Incorrect use of the Server Runtime Environment default encoding GBK, so normal display under the UTF-8 Chinese
Eventually displayed Garbled
.
Mule
In the cxfinboundmessageprocessor class 201st line:
MSG = out. tostring ();
This is Garbled
Encoding format is not specified when bytes are transferred as strings. GBK is used by default, which isWSDL
File Garbled
.
There are two solutions to this problem:
1. Change the default JVM character encoding.
2. Modify the source code of the cxfinboundmessageprocessor class.
The first solution is to change the server's Java Runtime Environment by modifying the server's startup parameters and appending a section "-dfile. Encoding = UTF-8. At this time, when we start the server, we will find that although our WSDL Chinese characters are no longer garbled, the server console displays Chinese
But all become Garbled
. I guess the server log output depends on the default Character Set of the operating system. Not to mention this log for the moment Garbled
Problem, even if this problem does not exist, I think this solution is not suitable. WSDL
Whether garbled characters depend on the default Character Set of the running server is uncontrollable for the oecp platform. What kind of character encoding should be used by the oecp platform depends on the application's own configuration.
So I decided to modify it. Mule
The lucky thing is that the cxfinboundmessageprocessor class has not been changed during the upgrade. Otherwise, it will be decompiled to obtain the source code.
Modify MSG = out. tostring (); of Line 1 to the following code:

Java code
    1. String ENC = event. getmulecontext (). getconfiguration (). getdefaultencoding ();
    2. MSG = out. tostring (ENC );

obtain the configured encoding format from mule
configuration, and specify this encoding format when converting stream to string. now the problem has been solved.
when using mule to publish a service, we can use the wsdldocumentation
label to add a description for the service. WSDL
is a description of WebService
. If there is no annotated text, WSDL
the description is not very readable. wsdldocumentation
the tag exists as this description, if you want to add the description of Chinese
to WSDL
, this label should be the only option. if you are using mule
encountered garbled characters
in Chinese
, hope the above content can help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.