Home  >  Q&A  >  body text

java - SpringMVC@ResponseBody中文乱码

PHP中文网PHP中文网2720 days ago688

reply all(6)I'll reply

  • 高洛峰

    高洛峰2017-04-18 10:44:03

    Just add this to the xml file
    <mvc:annotation-driven>

        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes" value="text/html;charset=UTF-8"></property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:44:03

    I don’t know what MediaType is, but I have always used the encoding produces = "application/json;charset=UTF-8", or the front-end encoding is not utf-8

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:44:03

    If the server is Tomcat, change to version 8.0 or above. Older versions did not encode UTF-8 by default.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:44:03

    Configure a spring encoding filter
    I searched for one for reference
    If it is a new project, check out spring boot. Basically, there is no need to configure it. Things like Chinese garbled characters are all configured by default.

    reply
    0
  • 阿神

    阿神2017-04-18 10:44:03

    Let’s talk about the garbled code problem GET and POST
    POST configure the spring filter in the web. URIencoding=utf-8 or take it and use ISO-8859-1 to manually decode it and then encode it in UTF-8

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:44:03

    Configuring a simple CharacterFilter may not solve the problem. If you are using Jackson2, try the following solution

    <mvc:annotation-driven>
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <util:list>
                            <value>application/json;charset=UTF-8</value>
                            <value>text/html;charset=UTF-8</value>
                        </util:list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>

    Jackson1 is the same, but the class is changed to MappingJacksonHttpMessageConverter

    reply
    0
  • Cancelreply