Spring MVC HTTP Status 406 - 解决方法
2017-04-06 20:34
323 查看
用的Spring版本是4.2.4,在用@ResponseBody标注返回json格式时候遇到这样的错误:“The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.”。字面意思很明显,产生的格式跟能接受的格式不符,对不上眼。但是各种google、stackoverflow之后,大多数答案都只是说要加上json的相关依赖之类的,试了都无用。于是跟踪代码,发现Spring默认ContentNegotiationManager使用org.springframework.web.accept.ServletPathExtensionContentNegotiationStrategy解析可接受的media
type,这貌似是Spring 3哪个版本以后开始的特性,因为我的的url-pattern 是*.htm格式的,于是解析出来能accept的media type只有text/html,自然报错。而另一个HeaderContentNegotiationStrategy根本没发挥作用。
一个可行的解决方案:
在spring-servelt中添加
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
这两个bean处理请求映射的,在bean中默认会创建一个使用HeaderContentNegotiationStrategy的ContentNegotiationManager,这样就能解析htttp请求头Accept中的类型了。但要注意的是,上述两个bean要定义在<mvc:annotation-driven/>之前。为什么呢?因为<mvc:annotation-driven/>会注册RequestMappingHandlerMapping, RequestMappingHandlerAdapter以及 ExceptionHandlerExceptionResolver等等的东西,如果上述两个bean定义放在<mvc:annotation-driven/>之后就不起作用了。
type,这貌似是Spring 3哪个版本以后开始的特性,因为我的的url-pattern 是*.htm格式的,于是解析出来能accept的media type只有text/html,自然报错。而另一个HeaderContentNegotiationStrategy根本没发挥作用。
一个可行的解决方案:
在spring-servelt中添加
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
这两个bean处理请求映射的,在bean中默认会创建一个使用HeaderContentNegotiationStrategy的ContentNegotiationManager,这样就能解析htttp请求头Accept中的类型了。但要注意的是,上述两个bean要定义在<mvc:annotation-driven/>之前。为什么呢?因为<mvc:annotation-driven/>会注册RequestMappingHandlerMapping, RequestMappingHandlerAdapter以及 ExceptionHandlerExceptionResolver等等的东西,如果上述两个bean定义放在<mvc:annotation-driven/>之后就不起作用了。
相关文章推荐
- SpringMVC 生成json报 HTTP Status 406.错误的解决方法
- HTTP Status 404(The requested resource is not available)的几种解决方法
- “HTTP Status 404 - No result defined for action ****** and result result”错误解决方法
- 解决 spring mvc3.1下post json出现HTTP Status 400 The request sent by the client was syntactically incorrect
- 出现HTTP Status 405 - HTTP method GET is not supported by this URL 原因及解决方法:
- HTTP status Code 412 未满足前提条件的解决方法之一
- HTTP Status 503 - Servlet action is currently unavailable解决方法
- “HTTP Status 404 - No result defined for action ****** and result result”错误解决方法
- HTTP Status 404(The requested resource is not available)的几种解决方法
- HTTP Status 404 - No result defined for action解决方法
- Spring MVC 通过@ResponseBody标签返回JSON数据 报406错误的解决方法
- HTTP Status 404 - Servlet action is not available 解决方法[转]
- HTTP Status 404(The requested resource is not available)的几种解决方法
- HTTP Status 404 - No result defined for action解决方法
- 在配置cas单点登录时报HTTP Status 500 - javax.net.ssl.SSLException 的解决方法
- jsp+tomcat+eclispe,http status 404解决方法
- HTTP Status 404 - No result defined for action解决方法
- 解决方法HTTP Status 500 - description The server encountered an internal error () that prevented it from fulfilling this request
- Ajax中XMLHttpRequest对象的status为0时的解决方法
- 访问jsp报HTTP Status 500 - java.lang.NullPointerException的解决方法