Spring Controllers注册了两次



在Spring MVC配置文件中,我有以下部分:

    <bean id="handler" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"
          p:alwaysUseFullPath="true"
          p:contentNegotiationManager-ref="contentNegotiationManager"
          p:useRegisteredSuffixPatternMatch="true" />

<mvc:annotation-driven />
<context:annotation-config/>
<context:component-scan base-package="com.tarhun.geo" use-default-filters="false">
     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

问题是控制器映射被注册了两次,即在日志中我看到每个映射消息是重复的:

注释。RequestMappingHandlerMapping:217 -映射"{[/rest/company/{companyId}],方法=[],参数=[],标题....

我认为自定义RequestMappingHandlerMapping定义存在问题。因为当我移除它-我的控制器只映射一次。但我仍然需要它,因为如果我删除它,我得到异常时,试图调用API: No mapping found...

你能给点建议吗?

仅供参考,我也有其他Spring上下文配置文件,但我确信它们不会第二次加载控制器(我甚至使用context:exclude=Controller来防止这种情况)。

在mvc配置中,组件被扫描两次,您不需要这样做。

在配置中,您可以设置注释驱动的配置,您的xml配置应该如下所示。

<aop:aspectj-autoproxy />
<mvc:annotation-driven> 
    <mvc:message-converters>
         <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> 
    </mvc:message-converters>
</mvc:annotation-driven> 
<context:component-scan base-package="com.tarhun.geo" />
<!-- UI resources exclusions from servlet mapping -->
<mvc:resources location="/ui/" mapping="/ui/**"/>

如果你只想加载Controller, Service或Repository,添加include/exclude过滤器是可选的

最新更新