【问题标题】:Spring MVC annotation error "No adapter for handler - Does your handler implement a supported interface like Controller?"Spring MVC 注释错误“处理程序没有适配器 - 您的处理程序是否实现了支持的接口,如 Controller?”
【发布时间】:2010-07-08 15:35:03
【问题描述】:

我有一个用 2.5 编写的现有 Spring MVC 应用程序。

我想使用新的注解控制器。我有点看到它非常灵活,可以满足我的其他需求。

我的问题是,我似乎不能同时使用它们。

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:component-scan
            base-package="com.test.web" />

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <!-- Controller Mappings Here -->
    <bean id="homeController" class="com.test.web.HomeController">
        <property name="cacheSeconds" value="120" />
    </bean> 

    //other plain old spring mvc controller

当我运行我的应用程序并点击主页时,出现以下错误:

javax.servlet.ServletException:处理程序没有适配器 [com.test.web.HomeController@cca07b]:您的处理程序是否实现了像 Controller 这样的受支持接口?

我不确定,但我认为有些矛盾。这是一个相当大的 Spring MVC 应用程序,我不想更改那些已经使用旧 Spring Base Controller 工作的模块。

我的目标是仅在我的新增强功能中使用注释控制器。

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    您无需声明DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter。上下文默认注册了这些,以及旧式控制器的适配器。

    当你像这样显式声明它们时,默认的会被删除,上下文将只支持你声明的那些。

    【讨论】:

    • 嗨,Skaffman,我尝试了你的建议,但是当我删除这些声明时,我得到了 404。我已经在我的映射中注释掉了控制器,认为它会自动检测我的@controller。
    猜你喜欢
    • 2015-07-20
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 2021-07-27
    • 2022-01-11
    • 1970-01-01
    相关资源
    最近更新 更多