首页

解决spring mvc访问“HTTP Status 406..description The resource identified by ...not acceptable according to the request "accept" headers”错误异常

标签:org.springframework,webmvc,HTTP,406,status,HttpMediaTypeNotAcceptableException,representation     发布时间:2017-06-25   

一、异常描述

通过springframework的webmvc注册接口接口回org.springframework.ui.ModelMap类型(返回类型为String可以正常)时,通过浏览器访问时报出“HTTP Status 406 - type Status report message description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().”提示,如下图所示

解决spring mvc访问“HTTP Status 406..description The resource identified by ...not acceptable according to the request "accept" headers”错误异常

将log4j的等级设置debug,后台日志报“org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation..org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation.. Null ModelAndView returned to DispatcherServlet with name 'springmvc': assuming HandlerAdapter completed request handling”,具体如下所示

[02:23:35] [DEBUG]   DispatcherServlet:  DispatcherServlet with name 'springmvc' processing GET request for [/xwood-test/generateCodex.do]@b@[02:23:35] [DEBUG]   DefaultListableBeanFactory:  Creating instance of bean 'verificationCodeController'@b@[02:23:35] [DEBUG]   DefaultListableBeanFactory:  Finished creating instance of bean 'verificationCodeController'@b@[02:23:35] [DEBUG]   DefaultAnnotationHandlerMapping:  Mapping [/generateCodex.do] to HandlerExecutionChain with handler [com.xwood.test.vcode.VerificationCodeController@14d733ca] and 1 interceptor@b@[02:23:35] [DEBUG]   DispatcherServlet:  Last-Modified value for [/xwood-test/generateCodex.do] is: -1@b@[02:23:35] [DEBUG]   HandlerMethodInvoker:  Invoking request handler method: public org.springframework.web.servlet.ModelAndView com.xwood.test.vcode.VerificationCodeController.generateCodex(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws javax.servlet.ServletException,java.io.IOException@b@[02:23:35] [DEBUG]   AnnotationMethodHandlerExceptionResolver:  Resolving exception from handler [com.xwood.test.vcode.VerificationCodeController@14d733ca]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation@b@[02:23:35] [DEBUG]   ResponseStatusExceptionResolver:  Resolving exception from handler [com.xwood.test.vcode.VerificationCodeController@14d733ca]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation@b@[02:23:35] [DEBUG]   DefaultHandlerExceptionResolver:  Resolving exception from handler [com.xwood.test.vcode.VerificationCodeController@14d733ca]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation@b@[02:23:35] [DEBUG]   DispatcherServlet:  Null ModelAndView returned to DispatcherServlet with name 'springmvc': assuming HandlerAdapter completed request handling@b@[02:23:35] [DEBUG]   DispatcherServlet:  Successfully completed request

二、解决方法

1.将spring-mvc.xml的配置annotation-driven等相关,之前配置如下

<?xml version="1.0" encoding="UTF-8"?>@b@<beans xmlns="http://www.springframework.org/schema/beans"@b@		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"@b@		xmlns:context="http://www.springframework.org/schema/context"@b@		xmlns:aop="http://www.springframework.org/schema/aop"@b@		xmlns:mvc="http://www.springframework.org/schema/mvc"@b@		xsi:schemaLocation="@b@			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd@b@			http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd@b@			http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd@b@			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">@b@		@b@		<!-- 自动扫描Action加入Spring上下文 Scope:prototype -->@b@		<context:component-scan base-package="com.xwood.test" scope-resolver="org.springframework.context.annotation.Jsr330ScopeMetadataResolver"/> @b@		 @b@</beans>

修改后如下,重启服务接口可以正常访问了,问题解决

<?xml version="1.0" encoding="UTF-8"?>@b@<beans xmlns="http://www.springframework.org/schema/beans"@b@		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"@b@		xmlns:context="http://www.springframework.org/schema/context"@b@		xmlns:aop="http://www.springframework.org/schema/aop"@b@		xmlns:mvc="http://www.springframework.org/schema/mvc"@b@		xsi:schemaLocation="@b@			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd@b@			http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd@b@			http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd@b@			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">@b@		@b@		<!-- 自动扫描Action加入Spring上下文 Scope:prototype -->@b@		<context:component-scan base-package="com.xwood.test" use-default-filters="false">@b@			<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>@b@			<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>@b@		</context:component-scan>@b@		@b@		<mvc:annotation-driven>@b@			<mvc:message-converters register-defaults="true">@b@				<!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->@b@				<bean class="org.springframework.http.converter.StringHttpMessageConverter">@b@			    	<constructor-arg value="UTF-8" />@b@				</bean>@b@				<!-- 将Jackson2HttpMessageConverter的默认格式化输出设为true -->@b@				<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">@b@	                <property name="prettyPrint" value="true"/>@b@	            </bean>			@b@	  		</mvc:message-converters>@b@		</mvc:annotation-driven>@b@		@b@		 @b@</beans>

备注, web.xml配置参考如下

<context-param>@b@		<param-name>contextConfigLocation</param-name>@b@		<param-value>@b@			classpath*:/spring-context.xml@b@		</param-value>@b@	</context-param>@b@	 @b@	<listener>@b@		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>@b@	</listener>@b@	 @b@	 <servlet>@b@        <servlet-name>springmvc</servlet-name>@b@        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>@b@        <init-param>@b@            <param-name>contextConfigLocation</param-name>@b@            <param-value>/WEB-INF/spring-mvc.xml</param-value>@b@        </init-param>@b@        <load-on-startup>1</load-on-startup>@b@    </servlet>@b@    @b@    <servlet-mapping>@b@        <servlet-name>springmvc</servlet-name>@b@        <url-pattern>/*</url-pattern>@b@    </servlet-mapping>