SSM framework: Solve the background data to the front desk Chinese garbled problem, using @responsebody return JSON Chinese garbled

Source: Internet
Author: User

Scene:

In the actual application scenario, we need to return data from the background to the foreground, if the returned data contains Chinese, it will often appear in the background query out is good, but the transmission back to the inexplicable garbled, and, we have clearly been in the Web. XML encoding filtering, But still garbled, make people very headache.

Workaround:

The first: This method, which is estimated by many people, is encoded in the @RequestMappering annotations of each method in the controller, as follows:

@RequestMapping (value = "/queryuserbyid", produces = "text/plain;charset=utf-8")

  This method can solve the problem of return garbled, but there is a problem is: need to write in each of the methods produces = "Text/plain;charset=utf-8"

This sentence set, so invisibly, let our code look, there are so some unsightly, as a lazy program ape, also will not allow us to continue to do write this code,

So there's a second way.

The second type: This method only needs to be configured once in the Spring-mvc.xml configuration file, compared to the first method

<!--Custom Message Converter encoding to resolve background transfer json back to foreground, Chinese garbled problem--<mvc:annotation-driven > <mvc:message-converters register-defaults= "true" > <bean class= "org.springframework.http.co Nverter. Stringhttpmessageconverter "> <property name =" Supportedmediatypes "> <list&                        Gt <value>application/json;charset=utf-8</value> <value>text/html;charset=utf-8</ Value> <!--application can be found in any Form form Enctype properties by default--<valu            E>application/x-www-form-urlencoded</value> </list> </property> </bean> <bean class= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConvert Er "></bean> </mvc:message-converters> </mvc:annotation-driven> 

  

SSM framework: Solve the background data to the front desk Chinese garbled problem, using @responsebody 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.