今天使用Spring mvc 尝试文件的上传下载,URL 访问的时候,报如下错误:

Type Exception Report 
 
Message No adapter for handler [com.cbss.FileUploadController@16d4015]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler 
 
Description The server encountered an unexpected condition that prevented it from fulfilling the request. 
 
Exception 
 
javax.servlet.ServletException: No adapter for handler [com.cbss.FileUploadController@16d4015]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler 
	org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:1202) 
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:947) 
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) 
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) 
	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) 
	javax.servlet.http.HttpServlet.service(HttpServlet.java:635) 
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) 
	javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
Note The full stack trace of the root cause is available in the server logs.

经过核查,发现原因如下,Spring 上下文配置文件没有如下配置,可能导致注解没有生效

<mvc:annotation-driven />

完整的applicationContext.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
                           http://www.springframework.org/schema/mvc 
                           http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> 
 
 
    <context:component-scan base-package="com.cbss" /> 
    <mvc:annotation-driven /> 
 
<!--    <!– 配置视图解析器 –> 
    <bean id="viewResolver" 
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/page/"></property> 
        <property name="suffix" value=".jsp"></property> 
    </bean>--> 
 
 
    <!--用于将对象转换成json--> 
    <bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> 
        <property name="supportedMediaTypes"> 
            <list> 
                <value>text/plain;charset=UTF-8</value> 
            </list> 
        </property> 
    </bean> 
 
    <bean id="jsonConverter" 
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
    </bean> 
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
        <property name="messageConverters"> 
            <list> 
                <ref bean="stringConverter"/> 
                <ref bean="jsonConverter"/> 
            </list> 
        </property> 
    </bean> 
 
    <bean id="multipartResolver" 
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
        <!--上传文件的最大大小--> 
        <property name="maxUploadSize" value="17367648787"></property> 
        <!-- 上传文件的编码 --> 
        <property name="defaultEncoding" value="UTF-8"></property> 
    </bean> 
 
</beans>







本文参考链接:https://blog.csdn.net/yulei_qq/article/details/79399808
评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

mybatis 开发环境搭建