最近使用spring4.0.5版本的Mvc,json请求时,客户端报错,406 Not Acceptable。
原因分析
在百度GOOGLE半天无果后,果断开始debug。
首先定位在org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor类中的下面代码处抛出了异常。
if (compatibleMediaTypes.isEmpty()) { throw new HttpMediaTypeNotAcceptableException(producibleMediaTypes); }
由此看来是compatibleMediaTypes为空导致的,由于之前一直使用spring3.1没有出现够相关的问题,于是切换到3.1继续debug,发现3.1的requestedMediaTypes为[*/*], 而3.2的requestedMediaTypes却为[text/html]。到此几乎可以确定在3.1版本与4.0.5版本中获取acceptableMediaTypes的方式肯定是有所差异的。
定位到每个版本的getAcceptableMediaTypes方法上,继续查看。
spring 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(); } }
spring 4.0.5
进入到getAcceptableMediaTypes方法,发现通过resolveMediaTypes获取MediaType的。
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); }
接着进入resolveMediaTypes方法
@Override public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException { for (ContentNegotiationStrategy strategy : this.contentNegotiationStrategies) { List<MediaType> mediaTypes = strategy.resolveMediaTypes(webRequest); if (mediaTypes.isEmpty() || mediaTypes.equals(MEDIA_TYPE_ALL)) { continue; } return mediaTypes; } return Collections.emptyList(); }
看来问题就是出在这里了。
解决办法
办法一
1、导入第三方的jackson包,jackson-mapper-asl-1.9.7.jar和jackson-core-asl-1.9.7.jar。
2、Spring配置文件添加:
<mvc:annotation-driven/> <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 --> </list> </property> </bean>
办法二
1、导入第三方的fastjson包,fastjson-1.1.34.jar
2、Spring配置文件添加:
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
相关推荐
Whether mandatory donation is acceptable or not?
## 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 ...
可用于Spring MVC框架,spring MVC中返回使用@ResponseBody注解返回时,后台没报错,就控制台显示406 Not Acceptable 原因是缺少jackson的包:jackson-core-asl-1.9.2.jar和jackson-mapper-asl-1.9.2.jar
spring MVC中返回使用@ResponseBody注解返回时,后台没报错,就控制台显示406 Not Acceptable 原因是缺少jackson的包:jackson-core-asl-1.9.2.jar和jackson-mapper-asl-1.9.2.jar
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.
406 Not Acceptable 指定的资源已经找到,但它的MIME类型和客户在Accpet头中所指定的不兼容(HTTP 1.1新)。 407 Proxy Authentication Required 类似于401,表示客户必须先经过代理服务器的授权。(HTTP 1.1新) ...
ANSI-ASHRAE Standard 62.1-2019 Ventilation for Acceptable Indoor
例如,它有助于正确地使用Unsupported Media Type (HTTP代码415)和Not Acceptable Unsupported Media Type (HTTP代码406)对无效请求做出响应。 您无需在每个请求上手动验证所有输入参数。 您可以配置服务支持...
node-contrib-mvcgenerator一种基于简单的JSON配置来搭建MVC项目的工具。 注意:此项目无需维护-此处存在更好的替代方法: : 配置文件示例: exports . config = { // filename extensions - acceptable formats: js...
一个简单的网络爬虫资源包,是之前在发现的,文件包好像还挺完整,本来应该免积分的,但是因为急需积分,所以设置为两积分,有兴趣的可以下载试试
不幸的是,这种方法存在问题:如果服务器需要返回406 Not Acceptable HTTP响应,则Play框架无法在响应中提供受支持的内容类型的列表,这对于开发人员编程客户端非常有帮助。用于您的Web应用程序。 play-content-...
5.4.7 406 Not Acceptable 115 5.4.8 407 Proxy Authentication Required 115 5.4.9 408 Request Timeout 116 5.4.10 409 Conflict 116 5.4.11 410 Gone 116 5.4.12 411 Length Required 116 5.4.13 413 Request ...
21.4.7 406 Not Acceptable 209 21.4.8 407 Proxy Authentication Required 209 21.4.9 408 Request Timeout 210 21.4.10 410 Gone 210 21.4.11 413请求实体过大。 210 21.4.12 414 Request-URI Too Long 210 21.4....
机架中间件拒绝蹩脚的 http 接受标头当请求不支持的类型时返回“406 Not Acceptable”状态。导轨用法 config.middleware.use Rack::Accepts执照版权所有 (c) 2010 Andrew Nesbitt 特此授予任何人免费获得本软件副本...
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 ...
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 ...
换一个windows服务器,发现就没这样的问题,仅在一台Linux服务器上可以重现,直观感觉就是这台Linux服务器某些配置有问题
部分国内外会议的LaTeX模板Lecture Notes in Computer Science (LNCS) ...Each contribution must be accompanied by a Springer copyright form, a so-called 'Consent to ... Modified forms are not acceptable.
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 ...
msg: "Not Acceptable" } 就是 没有想要的值 现在就说对 x-sign 的的地方 通过node进行js的处理 进行数据的调用可以返回数为 返回结果为 ———————————————— 版权声明:本文为CSDN博主「我来了007...