https://blog.csdn.net/helloman0908/article/details/84654341
Could not find acceptable representation
时间 2014-07-10 02:00:55 ITeye-博客 原文 http://jackface.iteye.com/blog/2090185
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
try {
List
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
List
return mediaTypes.isEmpty() ? Collections.singletonList(MediaType.ALL) : mediaTypes;
}
4.0.x
private List
List
return (mediaTypes.isEmpty() ? Collections.singletonList(MediaType.ALL) : mediaTypes);
}
看来问题就是出在这里了。不知Spring为何改变该实现方式??!!
解决方法如下:
第一种:
退回3.1.x
第二种:
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
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">
然后把
atom=application/atom+xml
html=text/html
json=application/json
*=*/*
三、第三种
详情见下面的地址 点击打开链接(主要针对3.2.X)
四、第四种
spring 3.2时requestedMediaTypes却为[text/html]的情况报406错误,还有一个原因可能是由于采用的后缀有关,如果使 用*.htm,*.html等,默认就会采用[text/html]编码,若改成*.json,*.shtml等就OK!!
---------------------
作者:helloman0908
来源:CSDN
原文:https://blog.csdn.net/helloman0908/article/details/84654341
版权声明:本文为博主原创文章,转载请附上博文链接!