Spring Boot Controller not mapping

Because of DemoApplication.class and HelloController.class in the same package
Locate your main application class in a root package above other classes
Take look at Spring Boot documentation Locating the Main Application Class

Using a root package also allows component scan to apply only on your project.

For example, in your case it looks like below:

com.webservice.demo.DemoApplication
com.webservice.demo.controller.HelloController


I too had the similar issue and was able to finally resolve it by correcting the source package structure following this

Your Controller classes are not scanned by the Component scanning. Your Controller classes must be nested below in package hierarchy to the main SpringApplication class having the main() method, then only it will be scanned and you should also see the RequestMappings listed in the console output while Spring Boot is getting started.

Tested on Spring Boot 1.5.8.RELEASE

But in case you prefer to use your own packaging structure, you can always use the @ComponentScan annotation to define your basePackages to scan.


In my case, it was missing the dependency from pom.xml, otherwise everything compiled just fine. The 404 and missing mappings info from Spring logs were the only hints.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

I also had trouble with a similar issue and resolved it using the correct package structure as per below. After correction, it is working properly. e.g.

  • Spring Application Main Class is in package com.example
  • Controller Classes are in package com.example.controller

Tags:

Java

Spring