Solution to the garbled json Chinese string returned during spring MVC Ajax request, springmvcjson

Source: Internet
Author: User

Solution to the garbled json Chinese string returned during spring MVC Ajax request, springmvcjson

The 1.org. springframework. http. converter. StringHttpMessageConverter class is a class that processes requests or strings, and the default character set is ISO-8859-1, so garbled characters appear when the returned json contains Chinese characters.

2. the parent class of StringHttpMessageConverter has a List <MediaType> supportedMediaTypes attribute to store the MediaType type that StringHttpMessageConverter supports special processing. If the MediaType type to be processed is not in the supportedMediaTypes List, the default character set is used.

3. Solution: Add the following code to the configuration file:

<! -- Solve the garbled problem when springmvc transmits json values --> <mvc: annotation-driven> <mvc: message-converters> <bean class = "org. springframework. http. converter. stringHttpMessageConverter "> <property name =" supportedMediaTypes "> <list> <value> application/json; charset = UTF-8 </value> </list> </property> </bean> </mvc: message-converters> </mvc: annotation-driven>

4. If you need to process other MediaType types, you can add other value tags to the list tag.


(Java) Chinese garbled characters in json format returned by jsp page ajax

Add
$. Ajax ({
ContentType: "application/json ",});

Springmvc js ajax Chinese garbled Problem

At the beginning of a little look at your problem, probably understand, garbled reasons are probably your server encoding problem, tomcat's default encoding is ISO-8859-1 this encoding, so if you need to change it, tomcat/conf/server. in xml, find Connector and add URIEncoding = "UTF-8" to it. If you don't change it, you have to return it in the action, that is, the receiver decoded with the ISO-8859-1, for example, new String (str. getBytes ("ISO-8859-1"), "UTF-8"); if an interceptor is used, it will be decoded in the interceptor. You can try it! Hope to 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.