SpringMvc JSON 406,吐血。。。。
解决方法一:
1、导入jackson-core-2.5.1.jar和jackson-databind-2.5.1.jar
2、Spring配置文件添加:
1 spring3为:org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
spring4为:org.springframework.http.converter.json.MappingJackson2HttpMessageConverter <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 这里可以直接转换成json数据-->
</list>
</property>
</bean>
------------------------------------------------------------------------------
<bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringConverter"/>
<ref bean="jsonConverter"/><!-- 这里出来的object-->
</list>
</property>
</bean>
虚线下面和上面的区别在于有没有加StringHttpMessageConverter以及有没有为MappingJacksonHttpMessageConverter加上property 属性,不加property 属性的话,页面不会显示undefined,会显示对象属性的值。
解决方法二:
1、导入第三方(阿里巴巴)的fastjson包,fastjson-1.2.7.jar
2、Spring配置文件添加:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
SpringMvc JSON 406,吐血。。。。的更多相关文章
- springmvc json 406
spring 4.0 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...
- springMvc中406错误解决,springMvc使用json出现406 (Not Acceptable)
springMvc中406错误解决, springMvc使用json出现406 (Not Acceptable) >>>>>>>>>>> ...
- springmvc报406错误
springmvc出现406,无非就两种情况,第一,百分之九十是json包没加进来,第二,百分之十,就是@ResponseBody返回的的数据,在请求的URL中不能有 .html的后缀
- springmvc遇见406错误的问题分析
如果springmvc遇到406错误: 90%没有加入Jackson的包 10%因为后缀为.html 10%的情况,解决方案为加多一个映射,使用.action
- springMVC+json构建restful风格的服务
首先.要知道什么是rest服务,什么是rest服务呢? REST(英文:Representational State Transfer,简称REST)描写叙述了一个架构样式的网络系统.比方 web 应 ...
- 关于springmvc json交互产生的406错误
产生错误的背景:springmvc使用<mvc:annotation-driven>进行配置,那么只要引入响应的json解析包就可以了.在pom中已经引入了如下: <dependen ...
- springmvc+json
1.在写我的springmvc demo时,由于要向前台返回相关信息,于是设置了@ResponseBody,但是要把对象转换成json格式,我却没有在xml文件里配置,所以报如下错误:HttpMedi ...
- SpringMvc Json LocalDateTime 互转,form urlencoded @ModelAttribute 转换
JDK8 的LocalDate 系列日期API ,比Date 或者 Calendar 都好用很多,但是在SpringMvc 自动装配会有点小问题 会导致抛出类似异常 default message [ ...
- SpringMVC @ResponseBody 406
使用@ResponseBody注解可以让Controller返回json格式的数据,在需要传输一个对象信息的时候往往使用这种方式.如果在使用的时候遇到了406,一般原因是: 缺少jar包.转换成jso ...
随机推荐
- Maven生命周期
Maven的生命周期抽象了构建的各个步骤,定义了他们的次序,但没有提供实现.Maven设计了插件机制.每个构建步骤都可以绑定一个或多个插件行为,而且Maven为大多数构建步骤编写并绑定了默认插件. M ...
- unity文件解析以及版本控制
刚开始使用unity做开发时,拿到一个范例工程先上传SVN,之后再自己做一些修改后,发现有非常多文件都有变化,这才知道有很多本地生成的文件,是不用上传的,但是不知道哪些才是需要共用的.之后又困扰于修改 ...
- C#求任意范围内的质数
class Program { public static List<int> list; static void Main(string[] args) { Console.WriteL ...
- Magento显示多货币,Magento 多货币设置
System - Configuration - Currency Setup 在右边Currency Options里的Allowed currencies勾选, 然后 System - Manag ...
- bootstrap总结
bootstrap在reset.css文件中设置margin为0,因此其他的标签需要重新设计margin. 1.h <h>标签和普通使用方法一样. 定义了.h1~.h6六个类名,样式和标题 ...
- C++学习笔记25:makefile文件2
Makefile文件语法 行解析:命令按行解析 命令行的行首字符为Tab键,其他行的行首字符不得为Tab键,但可以使用多个空格缩进 换行:命令太长时,行尾使用"\"换行 注释:行首 ...
- Qt4编码
#if QT_VERSION < 0x050000 qDebug() << "qt5以下的版本, 从QTextCodec设置全局字符集"; QTextCodec* ...
- PHP character garbled
MySql 控制台查询时出现乱码 Database&Table 的字符集 于Mysql控制台显示的字符集不一样 右键单击mysql控制台边框 单击属性 查看当前代码页的字符集模式是否于数 ...
- css的学习
第一天. css 1.知道 内联 内部 外部 的优先权 2.css的语法 3.id 选择器 以及 类选择器 和属性选择器 4.对图片 长 宽 的编辑 调整图片 5.通过内部 对整个页面 文字 颜 ...
- Poco C++ MySQl demo
#include "Poco/Exception.h"#include "Poco/Data/Session.h"#include "Poco/Dat ...