SpringMVC给前段返回的JSON数据中文乱码问题。

前端之家收集整理的这篇文章主要介绍了SpringMVC给前段返回的JSON数据中文乱码问题。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

由于SpringMVC的默认编码问题(不支持中文),所以在@ResponseBody里返回中文时会出现乱码。
方法一,在XML文档里写入全局设置,改为UTF8


  1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xsi:schemaLocation="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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  5. <context:component-scan base-package="base.code.controller"/>

  6. //UTF-8编码指定。
  7. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"

  8. id="internalResourceViewResolver">

  9. <property name="prefix" value="/WEB-INF/pages/"/>

  10. <property name="suffix" value=".html"/>

方法二,在@RequestMapping中添加produces = "text/html;charset=utf-8",但是次方法只适用于局部。

猜你在找的SpringMVC相关文章