Solve Springmvc's @responsebody return Chinese garbled

Source: Internet
Author: User

The reason that SPRINGMVC's @responsebody returned Chinese characters is that the SPRINGMVC default processing character set is Iso-8859-1, The following code can be seen in the spring Org.springframework.http.converter.StringHttpMessageConverter class:

public static final Charset Default_charset = Charset.forname ("iso-8859-1");

To solve the problem of returning Chinese garbled, there are two, the first is local, only for the return of a method of processing, the second is global, for the entire project, as follows:

The first: Add produces= "Text/html;charset=utf-8 in @requestmapping, such as:

@RequestMapping (value= "/login.do", method=requestmethod.post,produces= "Text/html;charset=utf-8") @ Responsebodypublic String Login (@RequestParam (value= "username") string username, @RequestParam (value= "password") String password) {return Jsonmessageutil.getsuccessjson ("login Successful");}

The second type: Add the following code to the Mvc:annotation-driven in the configuration file:

<mvc:annotation-driven ><!--Message Converter--><mvc:message-converters register-defaults= "true" ><bean class= "Org.springframework.http.converter.StringHttpMessageConverter" ><property name= "supportedmediatypes "Value=" Text/html;charset=utf-8 "/></bean></mvc:message-converters></mvc:annotation-driven ><mvc:resources location= "/resources/" mapping= "/resources/**"/>

For the garbled problem, so that the normal display of Chinese

Solve Springmvc's @responsebody return 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.