Talk about setting codes for JAXB and response to solve wechat4j Chinese garbled code

If there is a little partner as a programmer who says he has not encountered the problem of Chinese garbled code, I don't want to believe it. Today, when doing the smart Reply of wechat subscription number, I jumped into the fire pit of Chinese random code for a moment. When I first solved the problem, I cheered and totally forgot the pain she had brought me.

1、 Problem description

See, the random code in the red frame is naked and provocative to me, but I have nothing to do. It's terrible.

2、 Seek a solution

In the face of problems, we can only force ourselves to solve them with a knife. What can we do?

First of all, we must understand the mechanism of wechat intelligent reply. The drawing is as follows:

PS, the tools are not used well. Please forgive me.

Next, let's focus on where the importance of garbled code occurs.

1. The controller returns to the user

In this code, the encoding method of the specified response is UTF-8. It is reasonable to say that the problem of garbled code should be improved, but the result is still not.

2. Toxml of JAXB

Here are three key points:

1. m.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");

2. getXMLSerializer(os)

3. os. toString("UTF-8");

It can be seen that transcoding is involved in the above three places. In the first place, set Marshall's code; Second, set the encoding of the entire XML serializer; Third, set the string encoding of the returned bytearrayoutputstream. One of the three is indispensable.

This time so thoroughly, it should solve the problem, but still solve the Chinese garbled code, what should I do?

3. Tomcat output environment

In view of this, someone on the Internet provides such a solution.

set JAVA_ OPTS=%JAVA_ OPTS% %LOGGING_ MANAGER% -Dfile. encoding=UTF-8

After setting, restart Tomcat. The problem can be solved, but the side effect is that the output of the whole Tomcat running on the server (Tomcat CMD window) is always garbled. I don't think this scheme is desirable.

Add the following code to the running war

You'll be surprised to find that Tomcat's running environment (window server 2008) is GBK. I wonder if you're not surprised. I'm scared. Why not UTF-8? If it's GBK, I add more UTF-8 pages in the above two steps. Nonsense, I don't understand.

3、 Problem solving

With the above experience, we modify the following wechat4j code, mainly the second point.

In these two methods, we add configurable encoding methods to encoding, and you can manually set GBK (GBK is configured on my server), GB2312 and UTF-8.

In this way, we will find that the background output of wechat4j is no longer Chinese random code, but the information returned to the user is more chaotic.

How can you do this? Play with me, a programmer. I really want to spit out two dirty words. But don't be afraid. Since wechat4j's logger log is no longer garbled in Chinese, it can only be said that there is a problem in the first link.

Adjust it

Note that it can't be GBK, it can only be UTF-8. I don't know why. The product manager of wechat will explain it.

In particular, JAXB and response jointly declare the following solutions to wechat4j Chinese garbled Code:

WeChatController. Java is the URL you assigned to the wechat public development platform. The response is adjusted as follows

Jaxbparser for wechat4j Java, adjust the toxml (object obj) and getxmlserializer (OutputStream OS) methods respectively:

All right, it's all right.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>