question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

requestMappingHandlerMapping has 2 potential beans when actuator is active from spring boot version 2.7

See original GitHub issue

I 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:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dreis2211commented, Aug 2, 2022

Or alternatively handling just multiple beans of that type - that’s what we ended up doing. E.g.

@Service
public class VersionInfoService {

  List<RequestMappingHandlerMapping> handlerMappings;

  public VersionInfoService(List<RequestMappingHandlerMapping> handlerMappings) {
    this.handlerMappings = handlerMappings;
  }
}

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.

0reactions
turja12commented, Aug 4, 2022

Thanks for clarification, solution and extending release notes.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found