Spring MVC 3.x 版本使用 @ResponseBody 返回乱码

由于万恶的Spring MVC 默认返回编码 是 ISO-8859-1, 使用如下配置, 可以将编码专为UTF-8,

    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <bean class="org.springframework.http.MediaType">
                                <constructor-arg index="0" value="text" />
                                <constructor-arg index="1" value="plain" />
                                <constructor-arg index="2" value="UTF-8" />
                            </bean>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>

但是, 如果使用 POST 请求的时候 后台可能会报

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

因为请求的头信息中 可能请求返回的不是 text/plain;charset=UTF8  这种格式,需要吧request 的 Accept 设置为 text/plain;charset=UTF8.

时间: 2024-08-26 17:12:19

Spring MVC 3.x 版本使用 @ResponseBody 返回乱码的相关文章

Spring MVC学习之三:处理方法返回值的可选类型

转自:http://www.cnblogs.com/cuizhf/p/3810652.html ———————————————————————————————————————————————————————————— spring mvc处理方法支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void.下面将对具体的一一进行说明: ModelAndView @RequestMapping("/show1") publ

【Spring MVC】spring mvc中相同的url请求返回不同的结果

在项目中凡是使用Spring MVC这种控制器的,大多都是返回JSON数据对象,或者JSP页面. 但是相同的URL请求如何让他自动的选择放回的是什么? 在这里有由于鄙人没有亲自测试过,就不敢乱贴代码,只把别人写的引用一下,以防未来某天需要而忘却. 引用地址:http://my.oschina.net/wzlee/blog/262205

Spring mvc UEditor jsp版本

此文章主要解决在spring mvc 框架模式下面使用UEditor 图片或者文件上传成功读取不到的bug说明(主要已官方文档为主). 1.项目中应用UEditor.(jsp版本) 1.1参考官方 http://fex-team.github.io/ueditor/  中jsp使用说明. 2.结合Spring mvc 使用. 错误回顾,下图表示UEditor 引用成功,选择图片,图片不能正常显示,提示本地保存成功. 错误分析,启用firefox 的firebug 能看到一下图片 通过Everyt

Spring MVC 3 表单中文提交后乱码问题的解决方法

在spring mvc 3.0 框架中,通过JSP页面.HTML页面以POST方式提交表单时,表单的参数传递到对应的servlet后会出现中文显示乱码的问题.解决办法可采用spring自带的过滤技术,对所有页面间参数的传递设置统一的字符编码. 分两步解决问题: 1.设置页面格式为UTF-8 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 2.在web.xml中添加

Spring @ResponseBody 返回乱码 的优雅解决办法

版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 返回的结果中,中文全部被问号(?)代替的解决办法: *-servlet.xml的部分配置如下: [html] view plain copy <bean id="utf8Charset" class="java.nio.charset.Charset" factory-method="forName"> <constructor-arg value=&quo

Spring MVC系列:(11)返回JSON

1.引入jar包 jackson-core-asl-1.9.11.jar jackson-mapper-asl-1.9.11.jar 2.配置springmvc.xml     <!-- 基于注解的适配器 -->     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">         <property name=&qu

spring mvc 避免IE执行AJAX时,返回JSON出现下载文件

controller @RequestMapping(value = "/com/testjson", produces = { "text/plain;charset=UTF-8" }) public @ResponseBody String testjson(HttpServletRequest request){     Map<String,Object> m = new HashMap<String,Object>();     .

Spring MVC —— form表单post提交出现乱码

主要原因是:页面提交时,使用<contentType:utf-8/>格式,而服务端HttpMessageConverter解码时使用其它格式解码(如:ISO-8859-1)导致 解决方案:在Servlet中设置CharacterEncoding为UTF-8格式. 方法一:在Web.xml中加入Spring的字符集过滤器(已测) <filter> <filter-name>CharacterEncodingFilter</filter-name> <fi

Spring MVC rest接收json中文格式数据显示乱码

1.解决方法其中之一 在web.xml下添加配置: <!-- 编码配置 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name&g