Rest Controller not recognizing GET request in Spring Boot App
This what happens behind.
@SpringBootApplication
annotation is a combination of @Configuration
@EnableAutoConfiguration
@ComponentScan
.
@ComponentScan
without arguments tells the framework to find components/beans in the same package and its sub-packages.
Your Application
class which is annotated with @SpringBootApplication
is in the package com.nomad.dubbed.app
. So it scans that package and its sub-packages under it (like com.nomad.dubbed.app.*
). But your CircleController
is inside package com.nomad.dubbed.controller
which is not scanned by default. Your repositories too fall outside the default scan packages, so they too will not be discovered by spring framework.
So what to do now?, you have two options.
Option 1
Move the Application
class to the top directory(package). In your case com.nomad.dubbed
package. Then, since all controllers and other repositories are in sub-packages, they will be discovered by the framework.
Option 2
Use @ComponentScan
annotation with basePackages
argument, along with the @SpringBootApplication
in your Application
class like below.
@SpringBootApplication
@ComponentScan(basePackages="com.nomad.dubbed")
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
}
use a different url for your controller. "/" in spring-boot maps to static resources located in META-INF/resources and src/main/resources/static/ .
edit: forget above and do the following in your application class:
Application.java
package com.nomad.dubbed.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@ComponentScan("com.nomad.dubbed")
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
}
your rest controller is not discovered by spring-boots component scan. according to this doc http://docs.spring.io/spring-boot/docs/current/reference/html/… spring scans the packages below the package where the class with the @SpringBootApplication annotation resides. your controller is located in a parallel package.
We should not use @ComponentScan
annotation with @SpringBootApplication
, as that's not the right practice.
@SpringBootApplication
is a combination of 3 annotations: @ComponentScan
, @EnableAutoConfiguration
and @Configuration
.
Main class which has the @SpringBootApplication
annotation should be in parent/super package.
e.g. - com.spring.learning
is a parent package and its children are
com.spring.learning.controller
, com.spring.learning.service
, com.spring.learning.pojo
Hence it scans its package and sub packages.
This is the best practice – project layout or structure is a prominent concept in Spring Boot.