To create a Spring Boot project, let's generate a sample project from https://start.spring.io/. Here, you can add your required dependencies, such as Web, Thymeleaf, JPA , and DevTools. This can be done as follows:
- In the drop-down menus at the top, select Maven Project with Kotlin and Spring Boot 2.1.1 (SNAPSHOT):
- Give the name of Group, Artifact, Package Name, and add Dependencies. Then hit Generate Project.
- Download and unzip the project.
- Import the downloaded project into your IDE.
After following these steps, you're ready to use and modify the project. Let's see what's inside this project. You'll find a controller file under src/main/kotlin/{packageName}/AppController.kt.
Here's a piece of code from the controller file:
@RestController
class HtmlController {
@GetMapping("/")
fun blog(model: Model): String {
model["title"] = "Greeting"
return "index"
}
}
Create a class named HtmlController.kt and annotate it with the @RestController annotation to make it a controller class in which we'll deal with web requests. @RestController is the combination of @Controller and @ResponseBody.
Create a function named blog(model: Model) and annotate it with @GetMappingmaps("/"). This will return index.xml as output.