Several methods to solve SpringMvc background receiving json data Chinese garbled code problem

  • 2021-01-14 05:54:59
  • OfStack

1, When using ajax to transfer data from the foreground page to the background controller controller, Chinese garbled code (question mark???) appears. .

I found various solutions on the Internet before, but none worked. Finally, I found that the tomcat server was having problems receiving data

Solution:

Option 1: Transcode the parameters when they are received by controller


@ResponseBody
@RequestMapping(value="/getJsonDataByCityName",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");// To solve post The code 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 attribute URIEncoding=" UTF-8 "to this line of code


<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />

Reason analysis:

In fact, the cause of garbled code problem is due to the default tomcat configuration, receiving the request is ISO-8859-1 to transcode, resulting in Chinese garbled problem, as long as the correct utf-8 to transcode, can solve the garbled problem.

2. General data transmission, from jsp page to background controller, Chinese garbled code solution

(1) First, check whether the jsp page encoding format is utf-8

(2) Set Chinese filtering


<!--  Chinese code  -->
  <filter>
    <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 springMvc configuration file


<mvc:annotation-driven>
  <mvc:message-converters register-defaults="true">
    <!--  Start the Spring MVC The annotation function completes the request and annotation POJO The mapping of   Annotation request mapping 
     The default is ISO-88859-1 , avoid garbled code here set to UTF-8 -->
    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
      <property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
    </bean>
    <!--  Start the JSON Configuration of format , Automatically converts the format to JSON Format, no other classes are required  -->
    <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>

Related articles: