SPRINGMVC using AJAX requests to return Chinese garbled characters

Source: Internet
Author: User

Foreword: Recently, when writing a Java Web background, I adopted the Spring+mybatis+mysql method. Record the key issues that are encountered


interface returns data-relatedreturn null after using @responsebody Description: just run the background, happy to test the interface data, the result is to return null anyway ,finally through a variety of Baidu, found that the original is not introduced the key jar package. Workaround: The Jackson jar package (Jackson Core and Jackson Mapper) needs to be introduced into the Figure:


use @requestmapping to return Chinese garbled charactersCause Analysis: (Basic online is the same answer) First of all: It is determined (after many tests) that the Chinese characters are garbled only if the return value is String, and when the return value is map<string, object>, or other type, there is no Chinese garbled appearance.
and then find the reason: the reason is that this is one of spring MVC bug,spring MVC has a series of httpmessageconverter to deal with the return value of @responsebody annotations, If you return to list or otherwise use Mappingjacksonhttpmessageconverter and return string, use Stringhttpmessageconverter, This convert uses a character set that is iso-8859-1 and final. Therefore, garbled characters will appear when there is Chinese in the return JSON. [Java]View PlainCopy
    1. Public static final Charset Default_charset = Charset.forname ("iso-8859-1");


solution (and whether to try it successfully): tried many kinds of online methods, some simply useless, there are some when the client's accep is Application/json; Finally, it is a combination of client-side modifications to successfully solve the problem.
Environment: SPRINGMVC 3.1
the client is divided into three different types of requests:1. Direct get access in the browser, accept is "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"2. With jquery's JSONP Ajax request, accept is "*/*"3. With MUI Ajax request under h5+ environment, accept is "Application/json;charset=utf-8"(ps:h5+ is a recently done cross-platform mobile project development environment, in addition to have tried the default accept "Application/json" words regardless of the server side with which method, in the configuration, will return garbled, so finally had to manually add Charset=utf-8.)
Note: If nothing, the first and the second is the return of garbled, the third due to the manual return format, so there is no garbled, here is the third group to do the comparison because there are some methods will cause the third group can not access the normal. Try Method One: Add the following code to the Mvc:annotation-driven in the configuration file [HTML]View PlainCopy
  1. <mvc:annotation-driven >
  2. <!--message Converters --
  3. <mvc:message-converters register-defaults="true">
  4. <Bean class="Org.springframework.http.converter.StringHttpMessageConverter">
  5. <property name= "supportedmediatypes" value="text/html;charset=utf-8"/>
  6. </Bean>
  7. </mvc:message-converters>
  8. </mvc:annotation-driven>
principle: Stringhttpmessageconverter has a list<mediatype> supportedmediatypes attribute in the parent class to store The Stringhttpmessageconverter supports mediatype types that require special handling, with the default character set if the mediatype type to be processed is not in the Supportedmediatypes list. Final Result:Request Method 1 garbled language Request Method 2 Returns the correct Chinese Request Method 3 Returns the correct ChineseTry Method Two: Add the following code to the Mvc:annotation-driven in the configuration file [HTML]View PlainCopy
  1. <mvc:annotation-driven>
  2. <mvc:message-converters>
  3. <Bean class="Org.springframework.http.converter.StringHttpMessageConverter">
  4. <property name="Supportedmediatypes">
  5. <list>
  6. <span style="White-space:pre"> </span> <value> text/html; charset=UTF-8</value>
  7. <value>application/json; charset=UTF-8</value>
  8. <value>*/*; charset=UTF-8</value>
  9. </list>
  10. </Property>
  11. </Bean>
  12. </mvc:message-converters>
  13. </mvc:annotation-driven>
principle: The principle above, but here add a few value. Final Result:Request Method 1 garbled languageRequest Method 2 Returns the correct ChineseRequest Method 3 Returns the correct ChineseThe reason why this type of method can not be solved correctly, the reasons are different on the Internet, it is not easy to make a conclusion. In a word, this approach does not solve the problem at the moment.
Try method Three: Configuration in @requestmapping produces= "TEXT/HTML;CHARSET=UTF-8;" [Java]View PlainCopy
    1. @RequestMapping (value = "* * *", produces="TEXT/HTML;CHARSET=UTF-8;")

principle: manually to the corresponding accept return to develop format encoding data.

Final Result:Request Method 1returns the correct ChineseRequest Method 2 Returns the correct Chinese request Method 3 cannot request, out of the wrong, because produces did not add application/json; corresponding head.
Try method Four: Concurrent configuration in @requestmapping produces={"Application/json;", "Text/html;charset=utf-8;"} [Java]View PlainCopy
    1. @RequestMapping (value = "* * *", produces={"Application/json;","Text/html;charset=utf-8;"})

principle: manually to the corresponding accept return to develop format encoding data.

Final Result:Request Method 1 Return Chinese garbled request Method 2 return Chinese garbled request Method 3returns the correct Chinese
Try method Five: Concurrent configuration in @requestmapping produces={"text/html;charset=utf-8;", "Application/json;"} [Java]View PlainCopy
    1. @RequestMapping (value = "* * *", produces={"text/html;charset=utf-8;","Application/json;"})

Note: The difference between this and the previous method is that the order of the produces inside is reversed.
principle:Manually give the corresponding accept back the format encoding data.

Final Result:Request Method 1returns the correct ChineseRequest Method 2returns the correct ChineseRequest Method 3 Returns the correct Chinese

method Four and method Five contrast analysis:find produces set multiple accept only the first charset is useful,The following accept settings are valid (you cannot receive the corresponding accept request because it is not set), but the CharSet setting is not valid. Requires the client to manually make the CharSet.The exact reason is not clear (forgive me for not being proficient)
so the conclusion is:produces={"TEXT/HTML;CHARSET=UTF-8;", "Application/json;"}This setting, so that the normal browser request can display the Chinese language, and the client's impersonation request (can be Ajax or HTTP) manually specify the charset of accept, you can normally receive Chinese.

Original connection:

Springmvc encountered the pit, returned Chinese garbled and Ajax cross-domain

SPRINGMVC using AJAX requests to return Chinese garbled characters

Related Article

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.