加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 20:01:17 所属栏目:百科 来源:网络整理
导读:由于SpringMVC的默认编码问题(不支持中文),所以在@ResponseBody里返回中文时会出现乱码。 方法一,在XML文档里写入全局设置,改为UTF8 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/con

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


   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   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">

<context:component-scan base-package="base.code.controller"/>

//UTF-8编码指定。

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".html"/>

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读