Spring mvc 下Ajax获取JSON对象问题 406错误
2016-07-20 09:34
211 查看
我在学习springmvc过程中(我的项目是配置的后缀是.html),从controller返回对象。
如果我不使用 mvc-annotation-driver,而是手动配置,AnnotationMethodHandlerAdapter,然后在配置MappingJackson2HttpMessageConverter转换器,这样是可以正常将对象返回成json的。
但是我使用mvc-annotation-deiver,让spring帮我们注册AnnotationMethodHandlerAdapter,然后在配置MappingJackson2HttpMessageConverter转换器,这样返回对象,就会出现406错误。试了很久都没有找到解决办法,后来在网上搜索了,发现很多答案并不是适用于我这种样子的错误,后来找到下面的这个答案,发现真的有效,我将返回后缀.html改为了.shtml就正常了。
总结下:其实说简单点,真正原因就是,如果配置后缀是.html ,springmvc默认会采用[text/html]编码。所以,后缀使用别的后缀或者,不用后缀就可以了。
以下部分转载自:http://blog.sina.com.cn/s/blog_6316d5610102uy93.html
Spring 3.2.x通过@ResponseBody标签返回JSON数据的方法都报406错: Failed to load resource: the server responded with a status of 406 (Not Acceptable) 以及报错描述: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()
Spring 3.2 配置如下:
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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
mediaTypes配置:
atom=application/atom+xml
html=text/html
json=application/json
*=*/*
用@ResponseBody返回对象出现问题,报406错误!!!
最后发现是:spring 3.2时requestedMediaTypes却为[text/html]的情况报406错误,还有一个原因可能是由于采用的后缀有关,如果使用*.htm,*.html等,默认就会采用[text/html]编码,若把AJAX请求URL后面加上*.json,*.shtml等就OK!
可参考:http://blog.csdn.net/gbtyy/article/details/17165605
如果我不使用 mvc-annotation-driver,而是手动配置,AnnotationMethodHandlerAdapter,然后在配置MappingJackson2HttpMessageConverter转换器,这样是可以正常将对象返回成json的。
但是我使用mvc-annotation-deiver,让spring帮我们注册AnnotationMethodHandlerAdapter,然后在配置MappingJackson2HttpMessageConverter转换器,这样返回对象,就会出现406错误。试了很久都没有找到解决办法,后来在网上搜索了,发现很多答案并不是适用于我这种样子的错误,后来找到下面的这个答案,发现真的有效,我将返回后缀.html改为了.shtml就正常了。
总结下:其实说简单点,真正原因就是,如果配置后缀是.html ,springmvc默认会采用[text/html]编码。所以,后缀使用别的后缀或者,不用后缀就可以了。
以下部分转载自:http://blog.sina.com.cn/s/blog_6316d5610102uy93.html
Spring 3.2.x通过@ResponseBody标签返回JSON数据的方法都报406错: Failed to load resource: the server responded with a status of 406 (Not Acceptable) 以及报错描述: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()
Spring 3.2 配置如下:
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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
mediaTypes配置:
atom=application/atom+xml
html=text/html
json=application/json
*=*/*
用@ResponseBody返回对象出现问题,报406错误!!!
最后发现是:spring 3.2时requestedMediaTypes却为[text/html]的情况报406错误,还有一个原因可能是由于采用的后缀有关,如果使用*.htm,*.html等,默认就会采用[text/html]编码,若把AJAX请求URL后面加上*.json,*.shtml等就OK!
可参考:http://blog.csdn.net/gbtyy/article/details/17165605
相关文章推荐
- JAVA图片转为base64格式
- struts中上传文件配置
- 单工半双工和全双工
- java web 学习链接
- java float比较相等
- play框架orm层实现
- Quartz Spring与Spring Task
- Java开发之Spring MVC(三)
- A+B问题(c,c#,java实现)
- Java 内存分配全面浅析
- Java开发之Spring MVC(二)
- java设计模式之单例模式
- java web开发(一) Java Web开发框架对比
- java23种设计模式五(连载)
- eclipse常用快捷键
- springmvc ajax放回json中文乱码???
- Java中File类的使用
- java异常
- ATL模型转换技术详解
- java 的Collection接口实例详解