Several methods to solve the problem of Chinese garbled characters in the SpringMvc background receiving json data, springmvcjson
1. Chinese garbled characters (question mark ???).
I found a variety of solutions on the Internet before and found that the problem was caused by the tomcat server receiving data.
Solution:
Method 1: transcode parameters when the controller receives parameters
@ ResponseBody @ RequestMapping (value = "/CES", produces = "application/json") public String getJsonByName (HttpServletRequest request, HttpServletResponse response, @ RequestParam String city_name) throws ServletException, IOException {// response. setContentType ("text/html; charset = UTF-8"); // request. setCharacterEncoding ("UTF-8"); // solve the post garbled problem System. out. println (request. getCharacterEncoding (); city_name = new String (city_name.getBytes ("ISO-8859-1"), "UTF-8"); System. out. println ("city_name:" + city_name );}
Method 2:
Configure the service. xml file in the tomcat directory
Tomcat7/conf/server. xml
Add the encoding property of URIEncoding = "UTF-8" to this line of code
<Connector port="8080" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" URIEncoding="UTF-8" />
Cause analysis:
In fact, garbled problem occurs because of the default tomcat configuration, receive requests to the ISO-8859-1 to transcode, leading to Chinese garbled problem, as long as the correct to UTF-8 to transcode, it can solve the garbled problem.
2. Common data transmission, from jsp pages to backend controller, Chinese garbled Problem Solution
(1) first, check whether the jsp page encoding format is UTF-8
(2) set Chinese Filter
<! -- Chinese encoding --> <filter-name> characterEncodingFilter </filter-name> <filter-class> org. springframework. web. filter. characterEncodingFilter </filter-class> <init-param> <param-name> encoding </param-name> <param-value> UTF-8 </param-value> </init- param> <init-param> <param-name> forceEncoding </param-name> <param-value> true </param-value> </init-param> </filter> <filter-mapping> <filter-name> characterEncodingFilter </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>
(3) set JSON data conversion in the springMvc configuration file
<Mvc: annotation-driven> <mvc: message-converters register-defaults = "true"> <! -- Start the annotation function of Spring MVC, complete the request and annotation POJO poing annotation request ing is ISO-88859-1 by default, avoid garbled here set as UTF-8 --> <bean class = "org. springframework. http. converter. stringHttpMessageConverter "> <property name =" supportedMediaTypes "value =" text/html; charset = UTF-8 "/> </bean> <! -- Start JSON format configuration and automatically convert the format to JSON format without other classes --> <bean id = "jacksonMessageConverter" class = "org. springframework. http. converter. json. mappingJacksonHttpMessageConverter "> <property name =" supportedMediaTypes "value =" application/json; charset = UTF-8 "/> </bean> </mvc: message-converters> </mvc: annotation-driven>
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.