5

I'm trying to autowire org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping in my spring mvc controller in order to get all url mappings and display them on UI, but not successfull. There is error that the bean is missing:

 org.springframework.beans.factory.BeanCreationException: Could    not autowire field: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping web.controller.WorkController.handlerMapping; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

My web.xml:

<display-name>Spring MVC Application</display-name>

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

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/root-context.xml</param-value>
</context-param>

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

My mvc-dispatcher-servlet.xml:

 <context:annotation-config/>
    <context:component-scan base-package="web.controller"/>
             <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

My root-context.xml:

<bean id="helloBean" class="web.beans.HelloBean"/>

The java controller:

package web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import web.beans.HelloBean;

import java.util.List;

@Controller
public class WorkController {

    @Autowired RequestMappingHandlerMapping handlerMapping;
    @Autowired private HelloBean helloBean;
    @Autowired private ApplicationContext applicationContext;

    @RequestMapping(value = "/index")
    public String index() {
        return "index";
    }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Petr Sadf
  • 51
  • 1
  • 1
  • 3

4 Answers4

7

You should initiate the RequestMappingHandlerMapping bean before autowired it. It has two way:

  1. In springxml config such as hello bean
<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<!-- add your properties here property name="..." value="..."></property-->
</bean>
  1. Or using

    @Configuration

    @Configuration 
    @ComponentScan("your.package") 
    @EnableWebMvc   
    public class AppConfig {  
    ...
        @Bean
        public RequestMappingHandlerMapping requestMappingHandlerMapping() {
           RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
           // add properties here
           return mapping;
        }
    ...
    } 
    
Dmitriy
  • 109
  • 4
3

Try to get all request urls, The code below may be useful for you.

ServletContext servletContext = request.getSession().getServletContext();
WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
Map<String, HandlerMapping> allRequestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext, HandlerMapping.class, true, false);
for (HandlerMapping handlerMapping : allRequestMappings.values()) {
    if (handlerMapping instanceof RequestMappingHandlerMapping) {
          RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) handlerMapping;
          Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
          for (Map.Entry<RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodEntry : handlerMethods.entrySet()) {
             RequestMappingInfo requestMappingInfo = requestMappingInfoHandlerMethodEntry.getKey();
             PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
             String requestUrl = SetUtils.first(patternsCondition.getPatterns());
             System.out.println(requestUrl);
          }
    }
}

Frankly speaking, java reflect is a key point to get all request urls. if you look into the spring-mvc source deeply, you will find the implementation classes of HandlerMapping interface, such as

AbstractControllerUrlHandlerMapping, AbstractDetectingUrlHandlerMapping,
AbstractHandlerMapping, AbstractHandlerMethodMapping,
AbstractUrlHandlerMapping, BeanNameUrlHandlerMapping, 
ControllerBeanNameHandlerMapping, ControllerClassNameHandlerMapping,
DefaultAnnotationHandlerMapping, RequestMappingHandlerMapping,
RequestMappingInfoHandlerMapping, SimpleUrlHandlerMapping 
Javy
  • 944
  • 5
  • 9
2
  • i try to add "@EnableWebFlux" in my Main class and it works(in my situation).
  • so i think maybe "EnableWebMvc" works so..
  • it works fine for me, don`t ↓ me please :)
@EnableWebFlux(for webflux)
@EnableWebMvc(for commvc)
@SpringBootApplication
public class InstoreApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(InstoreApplication.class)......
    }
}
Santonio
  • 23
  • 4
  • ref: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-config-enable – Santonio Jun 13 '19 at 07:39
0

You can add these in *.properties file:

# Log restful end points
logging.level.web=TRACE
logging.level.org.springframework.web=TRACE