Solve spring MVC @ResponseBody return Chinese string garbled problem

Source: Internet
Author: User

The default processing string encoded as iso-8859-1 for spring MVC, which causes garbled characters, For specific reference, public static final Charset Default_charset in the Org.springframework.http.converter.StringHttpMessageConverter class = Charset.forname ("iso-8859-1");


Workaround:

The first method:

Add annotations for methods that need to return a string, as follows:

@RequestMapping (value= "/getusers", produces = "application/json; Charset=utf-8 ")
Public String Getalluser () throws Jsongenerationexception, Jsonmappingexception, IOException
{
list<user> users = Userservice.getall ();
Objectmapper om = new Objectmapper ();
System.out.println (om.writevalueasstring (users));
DataGrid dg = new DataGrid ();
Dg.setdata (users);
Return om.writevalueasstring (DG);
}

This method works only for a single invocation method.

The second method:

In the configuration file, add

<mvc:annotation-driven>
<mvc:message-converters register-defaults= "true" >
<bean class= "Org.springframework.http.converter.StringHttpMessageConverter" >
<property name= "supportedmediatypes" value = "Text/plain;charset=utf-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>


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

Solve spring MVC @ResponseBody return Chinese string garbled problem

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.