springMVC中ajax请求响应406

springMVC中ajax请求响应406

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.

意思是请求不被接受。出现这种情况一般都是用了@ResponseBody注解,而springMVC中返回值类型转换器配置有问题

[html] view plain copy
  1. <bean id="contentNegotiationManagerFactoryBean"  
  2.           class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">  
  3.         <property name="favorPathExtension" value="true"/><!--匹配后缀-->  
  4.         <property name="favorParameter" value="false"/>  
  5.         <property name="ignoreAcceptHeader" value="false"/>  
  6.         <property name="mediaTypes">  
  7.             <map>  
  8.                 <entry key="json" value="application/json"/>  
  9.             </map>  
  10.         </property>  
  11.     </bean>  
这段代码中的favorPathExtension是匹配请求的后缀,设置成true的时候是根据请求的后缀匹配响应的格式,如请求blog.json就会返回json格式。但是现在的请求路径是blog.html,在转换器中并没有配置html这个后缀对应的响应类型,所以就会报406的错误。解决这个问题,将favorPathExtension=false,禁用后缀匹配即可