Bruno Carneiro
Bruno Carneiro

Reputation: 343

Request mapping is not exposed when application is started

I have a controller class (@RestController) with one method to handle GET requests (@GetMapping). However, after the application is started, when I hit http://localhost:8080/actuator/mappings my request mapping is not there.

@RestController
public class SpringDemoController {

    @GetMapping("/home")
    public String home(){
        return "home";
    }
}

You can find the whole project here: https://github.com/brunogcarneiro/spring-demo

The mappings result are: (http://localhost:8080/actuator/mappings)

[
   {
      "handler":"Actuator web endpoint 'mappings'",
      "predicate":"{GET [/actuator/mappings], produces [application/vnd.spring-boot.actuator.v3+json || application/vnd.spring-boot.actuator.v2+json || application/json]}",
      "details":{...}
   },
   {
      "handler":"Actuator root web endpoint",
      "predicate":"{GET [/actuator], produces [application/vnd.spring-boot.actuator.v3+json || application/vnd.spring-boot.actuator.v2+json || application/json]}",
      "details":{...}
   },
   {
      "handler":"org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)",
      "predicate":"{ [/error], produces [text/html]}",
      "details":{...}
   },
   {
      "handler":"org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)",
      "predicate":"{ [/error]}",
      "details":{...}
   },
   {
      "handler":"ResourceHttpRequestHandler [classpath [META-INF/resources/webjars/]]",
      "predicate":"/webjars/**"
   },
   {
      "handler":"ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]",
      "predicate":"/**"
   }
]

Upvotes: 0

Views: 235

Answers (1)

Toni
Toni

Reputation: 5165

The problem is that SpringDemoController is not located within the package or subpackages of the @SpringBootApplication class. Consequently, the component scanning does not detect your controller class.

You have two options:

  • either move the controller package into the springdemo package
  • or customize the base packages to scan as shown below.
@SpringBootApplication(scanBasePackages = "com.example")
public class SpringDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringDemoApplication.class, args);
    }

}

Upvotes: 1

Related Questions