Home  >  Q&A  >  body text

java - spring responsebody 返回中文乱码

迷茫迷茫2719 days ago680

reply all(6)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:08:55

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

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:08:55

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

    <mvc:message-converters>

    goods

    reply
    0
  • PHPz

    PHPz2017-04-18 10:08:55

    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");

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:08:55

    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.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:08:55

    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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:08:55

    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. . .

    reply
    0
  • Cancelreply