Springmvc using @responsebody to return JSON Chinese garbled

Source: Internet
Author: User

Sometimes we find that we receive Chinese and return is a. This is really a problem with the egg, spring in the parsing string converter default encoding is actually iso-8859-1

/*** Implementation of {@linkHttpmessageconverter} that can read and write strings. * * <p>by default, this converter supports all media types ({@code& #42;& #47;& #42;}), * and writes with a {@codeContent-type} of {@codeText/plain}. This can is overridden * by setting the {@link#setSupportedMediaTypes Supportedmediatypes} property. * * @authorArjen Poutsma *@since3.0*/ Public classStringhttpmessageconverterextendsAbstracthttpmessageconverter<string> {     Public Static FinalCharset default_charset = Charset.forname ("iso-8859-1");

Now that the problem has been found, it must be a way to change, different versions of spring as if the method is not the same, many of the online said is spring3.*, and now Spring4 early came out

Change the way you can refer to

Http://stackoverflow.com/questions/3616359/who-sets-response-content-type-in-spring-mvc-responsebody

Http://www.cnblogs.com/chenying99/archive/2012/04/17/2453017.html

I now use the Spring4.2.5, the above said several methods have tried, finally found only these two can

Method one, using (produces = "application/json; Charset=utf-8 "):

    @RequestMapping (value= "/getusersbypage", produces = "application/json; Charset=utf-8 ")//    @RequestMapping ("/getusersbypage ")    @ResponseBody public    String Getusersbypage ( String page,string rows,string text,httpservletrequest request,httpservletresponse response) {

Method Two, add in Spring-mvc.xml: (recommend this)

    <!--set the encoding for message conversion to UTF-8 prevent Controller from returning garbled characters in Chinese -    <Beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">        < Propertyname= "Messageconverters">            <List>                <Beanclass= "Org.springframework.http.converter.StringHttpMessageConverter">                    < Propertyname= "Supportedmediatypes">                        <List>                            <value>Text/html;charset=utf-8</value>                        </List>                    </ Property>                </Bean>            </List>        </ Property>    </Bean>

Both of these methods are verified without problems.

Springmvc using @responsebody to return JSON Chinese garbled

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.