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