Could not find acceptable representation
Spring 4.0.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.x,不再报406了, 虽然换回 3.1不报错了,但还是想看看在处理ajax返回json数据的方式上两个版本到底有何区别,debug之。 debug到
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(T returnValue,MethodParameter returnType,ServletServerHttpRequest inputMessage,ServletServerHttpResponse outputMessage)
在以下代码处抛出了异常:
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
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
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; }
4.0.x
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.1.x
第二种:
<?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
<?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>修改成如下格式
<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>
三、第三种
详情见下面的地址 点击打开链接(主要针对3.2.X)
四、第四种
spring 3.2时requestedMediaTypes却为[text/html]的情况报406错误,还有一个原因可能是由于采用的后缀有关,如果使 用*.htm,*.html等,默认就会采用[text/html]编码,若改成*.json,*.shtml等就OK!!
相关推荐
Whether mandatory donation is acceptable or not?
专栏博文“DICOM:基于DCMTK实现C-FIND SCU”中对应的源代码。基于dcmtk开源库中的findscu工程,实现的简单的C-FIND SCU,用于示范如何使用dcmtk操作实现具体的DICOM应用。
ANSI-ASHRAE Standard 62.1-2019 Ventilation for Acceptable Indoor
/* Tell the user that we could not find a usable */ /* WinSock DLL. */ return 1; } /* Confirm that the WinSock DLL supports 2.2.*/ /* Note that if the DLL supports versions greater */ /* ...
Resizes Images to sizes acceptable by half life for transitioning into pldecal.wad files This code is incomplete... A Majority of the programming has been accomplished.
## Synopsis ... ## Motivation This was created because ElasticSearch 5 removed the ability to run ElasticSearch Head as an Elastic Plugin. This offers an alternative to self-hosting in your own web ...
A simulation to find the optimized sizes of microgrid components (PV and battery) constrained by a certain acceptable loss of load percentage and by budget. This simulation is written by Stefano ...
一个简单的网络爬虫资源包,是之前在发现的,文件包好像还挺完整,本来应该免积分的,但是因为急需积分,所以设置为两积分,有兴趣的可以下载试试
It is not anymore acceptable that, in the age of information technology, millions of Euros are wasted for unnecessarily repeated tests or that patients do not obtain appropriate care because relevant ...
Do not depend on the symbol being brought in transitively via headers not directly included. One exception is if Foo is used in myfile.cc, it's ok to #include (or forward-declare) Foo in myfile.h, ...
It is not anymore acceptable that, in the age of information technology, millions of Euros are wasted for unnecessarily repeated tests or that patients do not obtain appropriate care because relevant ...
Verification through inspection There are similarities with the development...acceptable technique for small designs, where the complete design could be tested exhaustively through a few simulation runs.
EPIMap was able to achieve the theoretical best performance for 9 out of 14 benchmarks, while EMS could not achieve the theoreti- cal best performance for any of the benchmarks. EPIMap achieves ...
acceptable results. Although users do not need to understand the underlying theory behind SVM, we brie y introduce the basics necessary for explaining our procedure. A classi cation task usually ...
5.4.31 488 Not Acceptable Here 122 x SIP: Understanding the Session Initiation Protocol 5.4.32 489 Bad Event 122 5.4.33 491 Request Pending 122 5.4.34 493 Request Undecipherable 122 5.5 Server Error ...
tems.It does not cover everything that could possibly be addressed.There is still considerable room for research in several areas,and a great deal of information needs to be catalogued.My hope is that...
This book could not have a more expert author. Aleksandar Prokopec contributed to some of the most popular Scala libraries for concurrent and parallel programming. He also invented some of the most ...
In Agile Information Security, James Fitzer ...What is Acceptable Risk? Compliance and Agility Implementing Controls, Prevention, Monitoring, and Response Final Thoughts Bibliography About the Author
换一个windows服务器,发现就没这样的问题,仅在一台Linux服务器上可以重现,直观感觉就是这台Linux服务器某些配置有问题
解决办法:照上述方法安装rpc.rstatd并开启即可监控Linux. 2. configure: error: no acceptable C compiler found in $PATH 原因:未安装gcc编译器. 在命令行里敲入gcc –v, 如果提示command not found 就表示...