Spring MVC Get请求 解决中文乱码问题

2018-06-29  本文已影响113人  Arison

关键知识点:StringHttpMessageConverter

代码结构:

image.png

问题描述:

@ResponseBody 输入到浏览器页面的时候,出现乱码!
控制器里面输入的字符是中文

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0">

    <filter>
        <filter-name>encodingFilter</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>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
    xmlns= "http://www.springframework.org/schema/mvc"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="com.gxut.spring" />
    
    <mvc:annotation-driven>  
        <mvc:message-converters>  
            <beans:bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                <beans:property name="supportedMediaTypes">  
                    <beans:list>  
                        <beans:value>text/plain;charset=UTF-8</beans:value>  
                        <beans:value>text/html;charset=UTF-8</beans:value>  
                        <beans:value>application/json;charset=UTF-8</beans:value>  
                    </beans:list>  
                </beans:property>
            </beans:bean>
        </mvc:message-converters>  
    </mvc:annotation-driven>

</beans:beans>

@Controller
@RequestMapping("/api")
public class TestController {

    @RequestMapping(value="/test", method=RequestMethod.GET,
            produces="application/json; charset=utf-8")
    public  @ResponseBody String test(@RequestParam(name = "text", defaultValue = "你好") String test) {
        System.out.println("test=" + test);
        return test;
    }
上一篇下一篇

猜你喜欢

热点阅读