SpringMvc returns @ResponseBody Chinese garbled

Use SpringMvc's @ResponseBody to return the type of specified data as the http body for external output. If there is Chinese in the returned content in the browser, there will be garbled characters. The project encoding and tomcat encoding have been set to utf-8, and the following returns is a string of Chinese garbled characters.
Java code Collection code
@RequestMapping("user/get_comment_list.do") 
    public @ResponseBody String getUserCommentList(Integer user_id,Byte type){ 
        HashMap<String, Object> map = new HashMap<String, Object>(); 
        map.put( "type", type); 
        map.put("user_id", user_id); 
        CommentActPojo actPojo = new CommentActPojo(); 
        List<CommentInfo> list = this.bo.getComList(map); 
        actPojo.setComments(list); 
        // System.out.println("Data:"+JsonUtil.toJson(actPojo));//Print data without Chinese garbled 
        return JsonUtil.toJson(actPojo); 



The version used by SpringMvc is 3.2.2. Later, I found some information on the Internet. Forced to specify the returned data type and character encoding in @RequestMapping, the Chinese garbled code is solved, as follows:
Java code Collection code
@RequestMapping(value="user/get_comment_list. do",produces = "application/json; charset=utf-8") The 


problem is coming. If there are many requests like this in the project, it will be cumbersome and cumbersome to configure produces for each request. Check the source code , found that the org.springframework.http.converter.StringHttpMessageConverter class is involved when spring processes ResponseBody, which sets defaultCharset to ISO-8859-1 in the default implementation. When the method marked with @RequestMapping is not configured with the produces attribute, the default encoding will be used automatically; if the produces attribute is configured, the write method in AbstractHttpMessageConverter will not be affected by supportedMediaTypes, and the header set by produce is assigned to contenttype. Modify the configuration of RequestMappingHandlerAdapter, springMvc.xml is as follows:
Java code Collection code
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans " 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
                        http://www.springframework.org/schema/mvc 
                        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 
     
    <!-- 必须放在<mvc:annotation-driven>之前 -->   
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">   
        <property name="messageConverters">   
            <list>   
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">   
                    <property name="supportedMediaTypes">   
                        <list>   
                            <value>text/plain;charset=UTF-8</value> 
                            <value>text/html;charset=UTF-8</value> 
                            <value>applicaiton/javascript;charset=UTF-8</value>   
                        </list>   
                    </property>   
                </bean>   
            </list>   
        </property>   
    </bean> 
     
    <!-- Scan project files--> 
    <context:component-scan base-package="com.tcl.club.core" /> 
    <context:component-scan base -package="com.cus.back" > 
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> 
    </context:component-scan> 
     
    <mvc:annotation-driven / > 
 
    <bean id="multipartResolver" 
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> 
     
     
    <!-- Resolution of the model view name, that is, adding prefixes and suffixes to the model view name, and the address entered in requestmapping Then automatically call this class for view resolution --> 
    <  bean id="viewResolver" 
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="viewClass" 
            value="org.springframework.web.servlet.view.JstlView" /> 
        <property name="prefix" value=" /WEB-INF/view/" /> 
        <property name="suffix" value=".jsp" /> 
    </bean> 
     
</beans>  


The above springMvc.xml file resets the encoding method of StringHttpMessageConverter without having to Set the produces attribute in a @RequestMapping. If the returned Jackson type data can be set as follows:
Java code collection code
<bean   
       class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">   
       <property name="   

               <value>application/json; charset=UTF-8</value>   
               <value>application/x-www-form-urlencoded; charset=UTF-8</value>   
           </list>   
       </property>   
</bean>  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326944751&siteId=291194637