0
votes

In my project, I used @Configuration, @EnableAutoConfiguration, @ComponentScan and ImportResource configuration with annotation. I did not used @SpringBootApplication, but application is built successfully without @SpringBootApplication annotation. I don't understand why @RestController class not invoked?

    @Configuration
@EnableAutoConfiguration(exclude = {
        //removed default db config
        DataSourceAutoConfiguration.class, XADataSourceAutoConfiguration.class})
@ComponentScan(basePackages = { "com.test.debasish.dummy" }, excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Test.class))})
@ImportResource( value = {"classpath*:*beans*.xml"})
public class TestApplication{
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}



@RestController
public class TestController {
  private static final String template = "Hello, %s!";
  private final AtomicLong counter = new AtomicLong();


  @GetMapping("/test")
  @ResponseBody
  public Greeting getResource(@RequestParam(name="name", required=false, defaultValue="Stranger") String name) {
    
    return new Greeting(counter.incrementAndGet(), String.format(template, name));
  }

}
2
Please read sping boot architecture more detail - Rajesh

2 Answers

1
votes

It works because @springbootapplication annotation is also include @Configuration,
@EnableAutoConfiguration, @ComponentScan annotations. See the below picture

https://i.stack.imgur.com/PKkb8.jpg

1
votes

You need to setup spring-webmvc for using @RestController.

Normally, it is done automatically by using spring-boot-starter-web.

More detail:

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.