android结合SpringMVC 采用spring3.0版遇到的问题

本文详细介绍了如何解决Spring 3.2版本中通过@ResponseBody标签返回JSON数据时出现的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

http://blog.youkuaiyun.com/gbtyy/article/details/17165605

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 plain copy
  1. if(compatibleMediaTypes.isEmpty()){
  2. thrownewHttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
  3. }

看来是compatibleMediaTypes为空导致。看debug信息,经过比较发现3.1的requestedMediaTypes为[*/*],而3.2的requestedMediaTypes却为[text/html],producibleMediaTypes都是[application/json],继而发现获取acceptableMediaTypes的方式3.1与3.2不同 3.1的

3.1的

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. privateList<MediaType>getAcceptableMediaTypes(HttpInputMessageinputMessage){
  2. try{
  3. List<MediaType>result=inputMessage.getHeaders().getAccept();
  4. returnresult.isEmpty()?Collections.singletonList(MediaType.ALL):result;
  5. }catch(IllegalArgumentExceptionex){
  6. if(logger.isDebugEnabled()){
  7. logger.debug("CouldnotparseAcceptheader:"+ex.getMessage());
  8. }
  9. returnCollections.emptyList();
  10. }
  11. }
3.2的

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. privateList<MediaType>getAcceptableMediaTypes(HttpServletRequestrequest)throwsHttpMediaTypeNotAcceptableException{
  2. List<MediaType>mediaTypes=this.contentNegotiationManager.resolveMediaTypes(newServletWebRequest(request));
  3. returnmediaTypes.isEmpty()?Collections.singletonList(MediaType.ALL):mediaTypes;
  4. }
看来问题就是出在这里了。不知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 plain copy
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  14. http://www.springframework.org/schema/mvc
  15. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

把spring-mvc-3.0.xsd 升级到spring-mvc-3.2.xsd

[html] view plain copy
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  14. http://www.springframework.org/schema/mvc
  15. http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

然后把<mvc:annotation-driven>修改成如下格式

[html] view plain copy
  1. <mvc:annotation-drivencontent-negotiation-manager="contentNegotiationManager"/>
  2. <beanid="contentNegotiationManager"class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
  3. <propertyname="favorPathExtension"value="false"/>
  4. <propertyname="favorParameter"value="false"/>
  5. <propertyname="ignoreAcceptHeader"value="false"/>
  6. <propertyname="mediaTypes">
  7. <value>
  8. atom=application/atom+xml
  9. html=text/html
  10. json=application/json
  11. *=*/*
  12. </value>
  13. </property>
  14. </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

综上,第四种方式最优。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值