java - spring responsebody 返回中文乱码
迷茫
迷茫 2017-04-18 10:06:55
0
6
729
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(6)
洪涛

It also depends on what encoding your web container is... It will be over if you just step on it.

迷茫

Encoding support for spring-mvc encoding adapter. I remember correctly that the tag name is:

<mvc:message-converters>

goods

PHPzhong
  1. spring mvc adds Jackson support

  2. Add spring mvc configuration

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg name="defaultCharset" value="UTF-8"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

Or override StringHttpMessageConverter

/* 将 */ public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
/* 改 */ public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
伊谢尔伦

Use other packet capture tools to test whether your return result is UTF-8 Chinese.
Problems often occur at the source or the last step.
If the page is garbled, your JSP page does not set charset "UTF-8".
If there are garbled characters in the browser console, use F12 Network and look at the response header Content-Type = text/html, Encoding and so on.
There is no problem with these links. It is probably a problem with the String environment settings. The previous answers are worth your reference.

大家讲道理

When using springmvc to return the json format in the project, the Chinese characters were garbled. I looked at the springmvc source code and found that the default encoding of the StringHttpMessageConverter class is ISO-8859-1 (tragedy, why does something as big as springmvc not use utf-8, I don’t understand)

Here is the solution,

springmvc configuration file:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
        <property name="messageConverters">   
            <list>   
                <bean class = "org.springframework.http.converter.StringHttpMessageConverter">   
                    <property name = "supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>   
                        </list>   
                    </property>   
                </bean>   
            </list>   
        </property>  
    </bean>
    <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/html; charset=utf-8</value> 
                        </list> 
                    </property> 
                </bean>
            </list>  
        </property> 
    </bean>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">   
        <property name="interceptors">  
            <list>     
            </list>  
        </property>  
    </bean>  

To use this configuration, delete the in springmvc and do not use the default initialization configuration of springmvc

Ty80

Have you added CharacterEncodingFilter? If you haven’t added it, try adding it. If it doesn’t work:
Which version of spirng and which version of tomcat are you using?
And:
It’s not as complicated as what’s said above. . . Looking at the pieces of xml is annoying. . .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template