700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Spring 3.2.* MVC通过Ajax获取JSON数据报406错误

Spring 3.2.* MVC通过Ajax获取JSON数据报406错误

时间:2024-03-16 20:46:22

相关推荐

Spring 3.2.* MVC通过Ajax获取JSON数据报406错误

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()){thrownewHttpMediaTypeNotAcceptableException(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 privateList<MediaType>getAcceptableMediaTypes(HttpInputMessageinputMessage){try{List<MediaType>result=inputMessage.getHeaders().getAccept();returnresult.isEmpty()?Collections.singletonList(MediaType.ALL):result;}catch(IllegalArgumentExceptionex){if(logger.isDebugEnabled()){logger.debug("CouldnotparseAcceptheader:"+ex.getMessage());}returnCollections.emptyList();}}

3.2的

[java]view plaincopy privateList<MediaType>getAcceptableMediaTypes(HttpServletRequestrequest)throwsHttpMediaTypeNotAcceptableException{List<MediaType>mediaTypes=this.contentNegotiationManager.resolveMediaTypes(newServletWebRequest(request));returnmediaTypes.isEmpty()?Collections.singletonList(MediaType.ALL):mediaTypes;}

看来问题就是出在这里了。不知Spring为何改变该实现方式??!!

解决方法如下:

一、第一种继续用Spring 3.1.4。

二、第二种

[html]view plaincopy <?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"xmlns:aop="/schema/aop"xmlns:tx="/schema/tx"xmlns:mvc="/schema/mvc"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans-3.0.xsd/schema/context/schema/context/spring-context-3.0.xsd/schema/aop/schema/aop/spring-aop-3.0.xsd/schema/tx/schema/tx/spring-tx-3.0.xsd/schema/mvc/schema/mvc/spring-mvc-3.0.xsd">

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

[html]view plaincopy <?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"xmlns:aop="/schema/aop"xmlns:tx="/schema/tx"xmlns:mvc="/schema/mvc"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans-3.0.xsd/schema/context/schema/context/spring-context-3.0.xsd/schema/aop/schema/aop/spring-aop-3.0.xsd/schema/tx/schema/tx/spring-tx-3.0.xsd/schema/mvc/schema/mvc/spring-mvc-3.2.xsd">

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

[html]view plaincopy <mvc:annotation-drivencontent-negotiation-manager="contentNegotiationManager"/><beanid="contentNegotiationManager"class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"><propertyname="favorPathExtension"value="false"/><propertyname="favorParameter"value="false"/><propertyname="ignoreAcceptHeader"value="false"/><propertyname="mediaTypes"><value>atom=application/atom+xmlhtml=text/htmljson=application/json*=*/*</value></property></bean>

三、第三种

详情见下面的地址点击打开链接

四、第四种

spring 3.2时requestedMediaTypes却为[text/html]的情况报406错误,还有一个原因可能是由于采用的后缀有关,如果使 用*.htm,*.html等,默认就会采用[text/html]编码,若改成*.json,*.shtml等就OK

文章来源:/gbtyy/article/details/17165605

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。