zl程序教程

您现在的位置是:首页 >  Java

当前栏目

spring mvc HTTP Status 406 错误

2023-02-18 16:29:46 时间

错误信息:

查看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需要定义在其之前呢。