热门搜索 :
考研考公
您的当前位置:首页正文

SpringMVC 返回中文字符串乱码

来源:东饰资讯网
image.png

1. 在方法上添加注解

此方法只针对单个调用方法起作用。

@RequestMapping(value="/demo", produces = "application/json; charset=utf-8")  
@ResponseBody  
public Object toLoginSubmit(HttpServletRequest request,HttpServletResponse response){  
    return "中文显示";  
}  

2. 全局配置字符串乱码处理

springmvc.xml中配置


<!-- 处理请求时返回字符串的中文乱码问题 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
Top