android结合SpringMVC 采用spring3.0版遇到的问题
2013-12-12 15:03
671 查看
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers
/article/10138231.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 () 于是,百度、Google了半天,发现遇到此问题的人挺多的,但是都是说什么添加Jackson什么的,我是采用的fastjson,换成Jackson尝试了半天均还是406。 后来在stackoverflow有人说是Spring
3.2的BUG,于是退回到3.1.*,不再报406了, 虽然换回3.1不报错了,但还是想看看在处理ajax返回json数据的方式上两个版本到底有何区别,debug之。 debug到org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(T returnValue,MethodParameter
returnType,ServletServerHttpRequest inputMessage,ServletServerHttpResponse outputMessage),在以下代码处抛出了异常:
[java] view
plaincopy
if (compatibleMediaTypes.isEmpty()) {
throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
}
看来是compatibleMediaTypes为空导致。看debug信息,经过比较发现3.1的requestedMediaTypes为[*/*],而3.2的requestedMediaTypes却为[text/html],producibleMediaTypes都是[application/json],继而发现获取acceptableMediaTypes的方式3.1与3.2不同
3.1的
3.1的
[java] view
plaincopy
private List<MediaType> getAcceptableMediaTypes(HttpInputMessage inputMessage) {
try {
List<MediaType> result = inputMessage.getHeaders().getAccept();
return result.isEmpty() ? Collections.singletonList(MediaType.ALL) : result;
} catch (IllegalArgumentException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not parse Accept header: " + ex.getMessage());
}
return Collections.emptyList();
}
}
3.2的
[java] view
plaincopy
private List<MediaType> getAcceptableMediaTypes(HttpServletRequest request) throws HttpMediaTypeNotAcceptableException {
List<MediaType> mediaTypes = this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request));
return mediaTypes.isEmpty() ? Collections.singletonList(MediaType.ALL) : mediaTypes;
}
看来问题就是出在这里了。不知Spring为何改变该实现方式??!!3.0和3.2这一块都不完美。
解决方案一、第一种 继续用Spring
3.1.4。
解决方案1.1:http://lucky-xingxing.iteye.com/blog/1666834
苦苦研究了两天,之前对于这个问题,The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request
"accept" headers ()。在网上苦寻答案无果,都说是缺少jackson-core-asl和jackson-mapper-asl两jar包,但是我已经添加了,而且版本从1.0.1到1.9.1都试过了,全无效果。前提配置文件以及action里面改写的全都正确。今天终于在一个外文网站找到答案,是由于spring版本的问题引起的。我之前一直用的是3.0.0的版本。就是因为这个版本的问题。于是果断去官网下载3.1.1版本的,然后jar替换,一切正常运行,成功返回json数据。
二、第二种
[html] view
plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
把spring-mvc-3.0.xsd 升级到 spring-mvc-3.2.xsd
[html] view
plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
然后把<mvc:annotation-driven>修改成如下格式
[html] view
plaincopy
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="false" />
<property name="ignoreAcceptHeader" value="false" />
<property name="mediaTypes" >
<value>
atom=application/atom+xml
html=text/html
json=application/json
*=*/*
</value>
</property>
</bean>
三、第三种
详情见下面的地址点击打开链接
四、第四种
spring 3.2时requestedMediaTypes却为[text/html]的情况报406错误,还有一个原因可能是由于采用的后缀有关,如果使用*.htm,*.html等,默认就会采用[text/html]编码,若改成*.json,*.shtml等就OK
eg: http://192.168.1.15:8080/tvportal/mobile/program/programsByChannel?channelId=102
改成:http://192.168.1.15:8080/tvportal/mobile/program/programsByChannel.json?channelId=102
综上,第四种方式最优。
/article/10138231.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 () 于是,百度、Google了半天,发现遇到此问题的人挺多的,但是都是说什么添加Jackson什么的,我是采用的fastjson,换成Jackson尝试了半天均还是406。 后来在stackoverflow有人说是Spring
3.2的BUG,于是退回到3.1.*,不再报406了, 虽然换回3.1不报错了,但还是想看看在处理ajax返回json数据的方式上两个版本到底有何区别,debug之。 debug到org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(T returnValue,MethodParameter
returnType,ServletServerHttpRequest inputMessage,ServletServerHttpResponse outputMessage),在以下代码处抛出了异常:
[java] view
plaincopy
if (compatibleMediaTypes.isEmpty()) {
throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
}
看来是compatibleMediaTypes为空导致。看debug信息,经过比较发现3.1的requestedMediaTypes为[*/*],而3.2的requestedMediaTypes却为[text/html],producibleMediaTypes都是[application/json],继而发现获取acceptableMediaTypes的方式3.1与3.2不同
3.1的
3.1的
[java] view
plaincopy
private List<MediaType> getAcceptableMediaTypes(HttpInputMessage inputMessage) {
try {
List<MediaType> result = inputMessage.getHeaders().getAccept();
return result.isEmpty() ? Collections.singletonList(MediaType.ALL) : result;
} catch (IllegalArgumentException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not parse Accept header: " + ex.getMessage());
}
return Collections.emptyList();
}
}
3.2的
[java] view
plaincopy
private List<MediaType> getAcceptableMediaTypes(HttpServletRequest request) throws HttpMediaTypeNotAcceptableException {
List<MediaType> mediaTypes = this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request));
return mediaTypes.isEmpty() ? Collections.singletonList(MediaType.ALL) : mediaTypes;
}
看来问题就是出在这里了。不知Spring为何改变该实现方式??!!3.0和3.2这一块都不完美。
解决方案一、第一种 继续用Spring
3.1.4。
解决方案1.1:http://lucky-xingxing.iteye.com/blog/1666834
苦苦研究了两天,之前对于这个问题,The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request
"accept" headers ()。在网上苦寻答案无果,都说是缺少jackson-core-asl和jackson-mapper-asl两jar包,但是我已经添加了,而且版本从1.0.1到1.9.1都试过了,全无效果。前提配置文件以及action里面改写的全都正确。今天终于在一个外文网站找到答案,是由于spring版本的问题引起的。我之前一直用的是3.0.0的版本。就是因为这个版本的问题。于是果断去官网下载3.1.1版本的,然后jar替换,一切正常运行,成功返回json数据。
二、第二种
[html] view
plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
把spring-mvc-3.0.xsd 升级到 spring-mvc-3.2.xsd
[html] view
plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
然后把<mvc:annotation-driven>修改成如下格式
[html] view
plaincopy
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="false" />
<property name="ignoreAcceptHeader" value="false" />
<property name="mediaTypes" >
<value>
atom=application/atom+xml
html=text/html
json=application/json
*=*/*
</value>
</property>
</bean>
三、第三种
详情见下面的地址点击打开链接
四、第四种
spring 3.2时requestedMediaTypes却为[text/html]的情况报406错误,还有一个原因可能是由于采用的后缀有关,如果使用*.htm,*.html等,默认就会采用[text/html]编码,若改成*.json,*.shtml等就OK
eg: http://192.168.1.15:8080/tvportal/mobile/program/programsByChannel?channelId=102
改成:http://192.168.1.15:8080/tvportal/mobile/program/programsByChannel.json?channelId=102
综上,第四种方式最优。
相关文章推荐
- android 编译好ffmpeg 3.0+ 后再jni中使用遇到的问题
- [札记]SSM开发框架集成问题,Spring4.3.x+Mybatis3.4.x+SpringMVC集成时遇到的jason转换问题
- Unity3D 原生Android结合UnityPlayerActivity开发遇到的问题
- spring ,springMVC,Mybatis和ehcache整合遇到的问题
- Spring、SpringMVC、Mybatis整合遇到的问题
- Spring结合mongotemplate遇到的问题
- SpringMVC项目接入Springfox实战遇到的问题集合
- springMVC配置spring AOP功能中遇到的问题
- [转]更新Android SDK到3.0版本时,遇到Failed to rename directory E:\android\tools to E:\android\temp\ToolPackage.old01问题
- Android SwipeRefreshLayout结合WebView使用实现以及可能遇到的问题
- maven 部署到 tomcat ,maven 结合spring,springmvc 出现的问题
- Android SDK到3.0版本时,遇到Failed to rename directory E:/android/tools to E:/android/temp/ToolPackage.old01问题
- 采用共享jar包部署struts2+spring集成项目会遇到的问题
- android notification和peddingintent结合使用遇到的问题
- 更新Android SDK到3.0版本时,遇到Failed to rename directory E:\android\tools to E:\android\temp\ToolPackage.old01问题
- 学习spring 3.0 security和MVC REST遇到的几个问题
- 使用phonegap进行移动跨平台在Android平台开发所遇到的问题
- 关于开发Android应用时写从图库选择头像功能时遇到的一些问题。
- 使用IDA调试Android原生程序中遇到的问题及解决方法
- SpringMVC框架中遇到的问题