解决后台传数据到前台中文乱码问题,使用@ResponseBody返回json 中文乱码

解决方法一:

 @RequestMapping(value="/getphone",produces = "text/plain;charset=utf-8")

   /**输入手机号码后判断手机号是否存在*/
    @RequestMapping(value="/getphone",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String getphone(String phone,HttpSession session){
        Users u=service.selectPhoneService(phone);
        if(u==null){//如果为空,则需要注册
            String str="请您先注册,再登录。";
            session.setAttribute("str", str);
            return "请您先注册,再登录。";
        }
        return "true";

  以上方法试过无效

方法二,在spring-mvc.xml中添加:

<!-- 处理请求返回json字符串的中文乱码问题 -->
    <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>

  

猜你喜欢

转载自www.cnblogs.com/itzyz/p/12153863.html