您的位置:首页 > 编程语言 > Java开发

ajax请求json字符串springMVC返回中文乱码解决办法

2018-09-27 19:47 274 查看

方法一: 

在@RequestMapping(value="/ValidMobile",produces = "text/html;charset=UTF-8")中,加上produces = "text/html;charset=UTF-8"。

[code]    @RequestMapping(value="/ValidMobile",produces = "text/html;charset=UTF-8")
@ResponseBody
public String validMobile(@RequestBody String param) {
User user = new Gson().fromJson(param, User.class);
ResponseBean rb = new ResponseBean;
rb.setResultMsg("中文乱码");
return new Gson().toJson(rb);
}

 

 

方法二(推荐):

在spring-servlet.xml配置文件中加入

[code]<!-- 注解驱动 -->
<mvc:annotation-driven>
<!-- 指定http返回编码格式 -->
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
<value>*/*;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: