Binding of @SpringBootApplication to @RestController in Java/Kotlin

Viewed 61

I'm implementing a REST API in Kotlin, using two tutorials (1, 2).

The first one doesn't even include a main function to be the app's entry point.
The second, however, does include the following file, which isn't connected in any way (at least not one that I see) to the controller.

// ClientApiApplication.kt

@SpringBootApplication
class ClientApiApplication

fun main(args: Array<String>) {
    runApplication<ClientApiApplication>(*args)
}

And this is the controller I'm using:

// GreetingController.kt

@RestController
class GreetingController {

    @GetMapping("/greeting")
    fun greeting() {
        return "Hello World"
    }

}

However, when I run ClientApiApplication it does recognise the controller.
So where is this binding taking place? Is this something that Spring does out of the box?

1 Answers

Under the hood, @SpringBootApplication is a composition of the @Configuration, @ComponentScan and @EnableAutoConfiguration annotations. With this default setting, Spring Boot will auto scan for components in the current package (containing the @SpringBoot main class) and its sub packages (source)

After some more research, apparently this is explained in multiple websites. It's a shame the basic tutorials don't refer to it as well *shrug*

Related