spring mvc HTTP Status 406 错误

  • 作者: 凯哥Java(公众号:凯哥Java)
  • 工作小总结
  • 时间:2018-01-01 10:11
  • 3120人已阅读
简介 错误信息:查看spring-mvc的配置文件:再次查看错误信息:description Theresourceidentifiedbythisrequestisonlycapableofgeneratingresponseswithcharacteristicsnotacceptableaccordingtotherequest"accept"headers().其字

🔔🔔好消息!好消息!🔔🔔

有需要的朋友👉:微信号 kaigejava2022

错误信息:



查看spring-mvc的配置文件:


再次查看错误信息:

description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().其字面意思:产生的格式跟能接受的格式不符。查询多种资料都说什么没有添加json支持的包。于是查看依赖:

存在的。

接着查找:

发现Spring默认ContentNegotiationManager使用org.springframework.web.accept.ServletPathExtensionContentNegotiationStrategy解析可接受的media type,这貌似是Spring 3哪个版本以后开始的特性解决方案:在spring-mvc.xml文件中添加:


<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>

说明:如果配置文件中使用了<mvc:annotation-driven/>。以上两个bean需要定义在其之前呢。


TopTop