@RequestMapping annotation not working if

2019-07-30 03:28发布

问题:

I'm using Spring of version 2.5.6 with <context:component-scan /> and @Autowired.

While I'd been using SimpleUrlHandlerMapping in my dispatcher context everything was OK - autowiring was working fine, routes was picking controllers in right way etc.

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="urlMap">
        <map>
            <entry key="/login/login.html" value-ref="loginController" />
            <entry key="/secured/index/index.html" value-ref="indexController" />
        </map>
    </property>
</bean>

Then I decided to use @RequestMapping insted of configuring my routes in a XML file.

@Controller("loginController")
public class LoginController {

    @RequestMapping("/login/login.html")
    public ModelAndView login() {
        ModelAndView model = new ModelAndView("login/login");
        return model;
    }

}

When I had rewritten the code to use @RequestMapping, problem started. The server (Tomcat) began to give me a "No mapping found for HTTP request with URI" error.

I've found that the solution was to insert an "<context:component-scan base-package="com.example.springwebapp.controller" />" element in my dispatcher context config.

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

    <!-- ============== -->
    <!-- URL Mapping    -->
    <!-- ============== -->

<!--    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> -->
<!--        <property name="urlMap"> -->
<!--            <map> -->
<!--                <entry key="/login/login.html" value-ref="loginController" /> -->
<!--                <entry key="/secured/index/index.html" value-ref="indexController" /> -->
<!--            </map> -->
<!--        </property> -->
<!--    </bean> -->

    <!-- ============ -->
    <!-- viewResolver -->
    <!-- ============ -->

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <context:component-scan base-package="com.example.springwebapp.controller" />

</beans>

I feel that this can be a bad solution because I have similar "<context:component-scan base-package="com.example.springwebapp" />" in my app context config file to handle all other dependencies in one shot.

<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">

    <import resource="./spring-security.xml"/>
    <import resource="../database/DataSource.xml"/>
    <import resource="../database/Hibernate.xml"/>

    <context:component-scan base-package="com.example.springwebapp" />

</beans>

The question is why <context:component-scan /> at application level isn't picking @RequestMapping annotations right? I know that I can limit component scan by base-package at app level to certain package, but my intention was to use only one component-scan at app level and nothing more. Do I really have to use two distinct component-scan elements or maybe I'm missing something?

Here is my web.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <display-name>SpringWebApp</display-name>

    <!-- ========== -->
    <!-- Spring MVC -->
    <!-- ========== -->

    <welcome-file-list>
        <welcome-file>welcome.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>Application</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/spring/mvc-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Application</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/spring/application-context.xml
        </param-value>
    </context-param>

    <!-- =============== -->
    <!-- Spring Security -->
    <!-- =============== -->

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

回答1:

Try by adding this tag to your app context :

<mvc:annotation-driven />

EDIT

For Spring 2.5 try by adding the annotation config tag, a DefaultAnnotationHandlerMapping and a AnnotationMethodHandlerAdapter bean to your app context :

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


回答2:

After reading the answer to my question (many thanks to Jean-Philippe Bond) I decided to change my approach. I understood that having two separate context:component-scan is the proper way.

Now my app context has got:

<context:component-scan base-package="com.example.springwebapp" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Service" type="annotation"/>
        <context:include-filter expression="org.springframework.stereotype.Repository" type="annotation"/>
</context:component-scan>

and my dispatcher context has got:

<context:component-scan base-package="com.example.springwebapp" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>