requestMappingHandlerMapping has 2 potential beans when actuator is active from spring boot version 2.7
See original GitHub issueI did small sample project to show behavior which we are getting on bigger project. We are using actuator endpoints and same time we have a service where we are autowiring RequestMappingHandlerMapping. Our code was working in spring boot 2.6.x, but in version 2.7.x (even in latest 2.7.2) it is not working. When actuator is not put as dependency then this autowiring works, but in our case application will not start and we are getting this error:
Parameter 0 of constructor in com.example.demo.VersionInfoService required a single bean, but 2 were found:
- requestMappingHandlerMapping: defined by method 'requestMappingHandlerMapping' in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
- controllerEndpointHandlerMapping: defined by method 'controllerEndpointHandlerMapping' in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/WebMvcEndpointManagementContextConfiguration.class]
sample application for reproducing build.gradle
plugins {
id 'org.springframework.boot' version '2.7.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
}
VersionInfoService.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@Service
public class VersionInfoService {
RequestMappingHandlerMapping handlerMapping;
@Autowired
public VersionInfoService(RequestMappingHandlerMapping handlerMapping) {
this.handlerMapping = handlerMapping;
}
}
Demo1Application.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
Issue Analytics
- State:
- Created a year ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Remove redundant override of ...
SpringBoot 2.6.3 Redundant RequestMappingHandlerMapping bean configuration, MvcUriComponentsBuilder can work in multiple ...
Read more >Spring Boot Reference Documentation
Best practices: Code Structure | @Configuration | @EnableAutoConfiguration | Beans and Dependency Injection. Running your code: IDE | Packaged | Maven | Gradle....
Read more >spring-projects/spring-boot - Gitter
I'm doing some test with existing projects by migrating those to spring-boot-starter-paren version 2.0.0.M7 one thing I noticed is that service fails with...
Read more >Spring Boot Actuator Not Working - Stack Overflow
With the exact dependencies that you've listed, I get a ClassNotFoundException for RequestMappingHandlerMapping that causes startup to fail.
Read more >How to Enable All Endpoints in Spring Boot Actuator - Baeldung
Starting with Spring Boot 2, we have to enable and expose our endpoints. By default, all endpoints but /shutdown are enabled and only...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Or alternatively handling just multiple beans of that type - that’s what we ended up doing. E.g.
Plus whatever changes are needed that call the handlerMapping. In our case these were relatively straight-forward - we’re just looking in a list of mappings instead of a single one now.
Thanks for clarification, solution and extending release notes.