Spring-boot: No qualifying bean of type [org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping]

Created on 26 Dec 2014  路  8Comments  路  Source: spring-projects/spring-boot

Following this question here:

http://stackoverflow.com/questions/26890727/spring-boot-issue-while-using-smvcurl-tag

I get the same error:

2014-12-26 12:27:22,489 [http-nio-8112-exec-5] ERROR: Servlet.service() for servlet jsp threw exception
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping] is defined: expected single matching bean but found 4: fallbackMapping,endpointHandlerMapping,repositoryExporterHandlerMapping,requestMappingHandlerMapping
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:365)
bug

Most helpful comment

This is covered in the reference documentation:

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc. If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Bean of type WebMvcConfigurerAdapter, but without @EnableWebMvc.

All 8 comments

What version of Spring Boot are you using?

spring-boot-1.2.0.RELEASE

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping] is defined: expected single matching bean but found 2: endpointHandlerMapping,requestMappingHandlerMapping
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:298)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968)
    at org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.getRequestMappingInfoHandlerMapping(MvcUriComponentsBuilder.java:340)
    at org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.fromMappingName(MvcUriComponentsBuilder.java:242)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)`

I got the 1.2.1 release, and I can see the @Primary annotation but I still get this exception:

    Stacktrace:] with root cause
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping] is defined: expected single matching bean but found 4: fallbackMapping,endpointHandlerMapping,repositoryExporterHandlerMapping,requestMappingHandlerMapping
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:365)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968)
    at org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.getRequestMappingInfoHandlerMapping(MvcUriComponentsBuilder.java:360)
    at org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.fromMappingName(MvcUriComponentsBuilder.java:262)

@paranoiabla I can't reproduce the failure with 1.2.1.RELEASE. I have a controller:

@RestController
@RequestMapping("/")
public class MyController {

    @RequestMapping("mapping")
    public String mapping() {
        return MvcUriComponentsBuilder.fromMappingName("MC#mapping").build();
    }

}

And this test that passes with 1.2.1.RELEASE:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Gh2237Application.class)
@IntegrationTest("server.port=0")
@WebAppConfiguration
public class Gh2237ApplicationTests {

    @Autowired
    private ApplicationContext context;

    @Value("${local.server.port}")
    private int port;

    private RestTemplate restTemplate = new TestRestTemplate();

    @Test
    public void primaryRequestMappingHandlerMapping() {
        assertEquals(4, this.context.getBeansOfType(RequestMappingHandlerMapping.class).size());

        ResponseEntity<String> entity = this.restTemplate.getForEntity(
                "http://localhost:" + this.port + "/mapping", String.class);
        assertEquals(HttpStatus.OK, entity.getStatusCode());
        assertEquals("http://localhost:" + this.port + "/mapping", entity.getBody());
    }
}

It fails if I drop back to 1.2.0.RELEASE with the same four beans your app has:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping] is defined: expected single matching bean but found 4: fallbackMapping,endpointHandlerMapping,repositoryExporterHandlerMapping,requestMappingHandlerMapping

If you'd like us to investigate further please provide us with a small sample that reproduces the problem on 1.2.1.RELEASE.

Hi @philwebb It works now. Turns out I had forgotten the @EnableWebMVC annotation so spring-boot was not kicking it. After removing the @EnableWebMVC it works now. Thanks to @olivergierke for the tip :)

Why adding @EnableWebMVC cause this problem?
I need to customize my mvc configuration, so i add @EnableWebMVC on my configuration class, it cause the problem outlined here...
so how to keep @EnableWebMVC and prevent this problem?
Thanks

This is covered in the reference documentation:

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc. If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Bean of type WebMvcConfigurerAdapter, but without @EnableWebMvc.

Was this page helpful?
0 / 5 - 0 ratings