Explain in detail the Chinese garbled code problem of JSON data returned by spring mvc3 and solve it
After checking some information on the Internet, I feel more complicated. Here, I use two very simple methods to solve the problem of Chinese garbled code.
Spring version: 3.2 2.RELEASE
Jackson JSON version: 2.1 three
Solution: in the controller method, write string JSON data to the network flow directly through response.
Use Jackson's objectmapper to convert Java objects to JSON data of string type.
In order to avoid Chinese random code, it is necessary to set character coding format, such as UTF-8, GBK, etc.
The code is as follows:
There is another method: set the @ requestmapping products parameter. The code is as follows:
Idea: use @ ResponseBody annotation to directly return JSON string. In order to prevent Chinese garbled code, set @ requestmapping's produces parameter to "text / HTML; charset = UTF-8".
The operation results are shown in the figure below:
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.