# Create a Spring Boot project with Kotlin The first part of the tutorial shows how to create a Spring Boot project with Gradle in IntelliJ IDEA using the Project Wizard. Note: This tutorial doesn't require using Gradle as the build system. You can follow the same steps if you use Maven. ## Before you start Download and install the latest version of [IntelliJ IDEA](https://www.jetbrains.com/idea/download/) and use an Ultimate subscription. Tip: If you use IntelliJ IDEA without an Ultimate subscription or you use another IDE, you can generate a Spring Boot project using a [web-based project generator](https://start.spring.io/#!language=kotlin&type=gradle-project-kotlin). ## Create a Spring Boot project Create a new Spring Boot project with Kotlin by using the Project Wizard in IntelliJ IDEA: 1. In IntelliJ IDEA, select File | New | Project. 2. In the panel on the left, select Spring Boot in the Generators section. 3. Specify the following fields and options in the New Project window: * Name: demo * Language: Kotlin * Type: Gradle - Kotlin Tip: This option specifies the build system and the DSL. * Package name: com.example.demo * JDK: Java JDK Note: This tutorial uses Amazon Corretto version 23. If you don't have a JDK installed, you can download it from the dropdown list. * Java: 17 Tip: If you don't have Java 17 installed, you can download it from the JDK dropdown list. ![Create Spring Boot project](images/create-spring-boot-project.png) 4. Ensure that you have specified all the fields and click Next. 5. Select the following dependencies that will be required for the tutorial: * Web | Spring Web * SQL | Spring Data JDBC * SQL | H2 Database ![Set up Spring Boot project](images/set-up-spring-boot-project.png) 6. Click Create to generate and set up the project. Tip: The IDE will generate and open a new project. It may take some time to download and import the project dependencies. 7. After this, you can observe the following structure in the Project view: ![Set up Spring Boot project](images/spring-boot-project-view.png)The generated Gradle project corresponds to the Maven's standard directory layout: * There are packages and classes under the `main/kotlin` folder that belong to the application. * The entry point to the application is the `main()` method of the `DemoApplication.kt` file. ## Explore the project Gradle build file Open the `build.gradle.kts` file: it is the Gradle Kotlin build script, which contains a list of the dependencies required for the application. The Gradle file is standard for Spring Boot, but it also contains necessary Kotlin dependencies, including the [kotlin-spring Gradle plugin](kotlin-spring-plugin.html) – `kotlin("plugin.spring")`. Here is the full script with the explanation of all parts and dependencies: ```KOTLIN // build.gradle.kts plugins { kotlin("jvm") version "2.3.21" // The version of Kotlin to use kotlin("plugin.spring") version "2.3.21" // The Kotlin Spring plugin id("org.springframework.boot") version "4.1.1" id("io.spring.dependency-management") version "1.1.7" } group = "com.example" version = "0.0.1-SNAPSHOT" java { toolchain { languageVersion = JavaLanguageVersion.of(17) } } repositories { mavenCentral() } dependencies { implementation("org.springframework.boot:spring-boot-h2console") implementation("org.springframework.boot:spring-boot-starter-data-jdbc") implementation("org.springframework.boot:spring-boot-starter-webmvc") implementation("org.jetbrains.kotlin:kotlin-reflect") // Kotlin reflection library, required for working with Spring implementation("tools.jackson.module:jackson-module-kotlin") // Jackson extensions for Kotlin for working with JSON runtimeOnly("com.h2database:h2") testImplementation("org.springframework.boot:spring-boot-starter-data-jdbc-test") testImplementation("org.springframework.boot:spring-boot-starter-webmvc-test") testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") testRuntimeOnly("org.junit.platform:junit-platform-launcher") } kotlin { compilerOptions { freeCompilerArgs.addAll("-Xjsr305=strict", "-Xannotation-default-target=param-property") // `-Xjsr305=strict` enables the strict mode for JSR-305 annotations } } tasks.withType { useJUnitPlatform() } ``` As you can see, there are a few Kotlin-related artifacts added to the Gradle build file: 1. In the `plugins` block, there are two Kotlin artifacts: * The `kotlin("jvm")` plugin defines the version of Kotlin to be used in the project. * The Kotlin Spring compiler plugin, `kotlin("plugin.spring")`, adds the `open` modifier to Kotlin classes to make them compatible with Spring Framework features. 2. In the `dependencies` block, a few Kotlin-related modules are listed: * The `tools.jackson.module:jackson-module-kotlin` module adds support for serialization and deserialization of Kotlin classes and data classes. * `org.jetbrains.kotlin:kotlin-reflect` is a Kotlin reflection library that enables full support for the [reflection features](reflection.html). 3. After the dependencies section, you can see the `kotlin` plugin configuration block. This is where you can add extra arguments to the compiler to enable or disable various language features. Learn more about the Kotlin compiler options in [Compiler options in the Kotlin Gradle plugin](gradle-compiler-options.html). ## Explore the generated Spring Boot application Open the `DemoApplication.kt` file: ```KOTLIN // DemoApplication.kt package com.example.demo import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication @SpringBootApplication class DemoApplication fun main(args: Array) { runApplication(*args) } ``` Declaring classes – class DemoApplication : Right after package declaration and import statements you can see the first class declaration, `class DemoApplication`. : In Kotlin, if a class doesn't include any members (properties or functions), you can omit the class body (`{}`) for good. @SpringBootApplication annotation : [@SpringBootApplication annotation](https://docs.spring.io/spring-boot/reference/using/using-the-springbootapplication-annotation.html#using.using-the-springbootapplication-annotation) is a convenience annotation in a Spring Boot application. It enables Spring Boot's [auto-configuration](https://docs.spring.io/spring-boot/reference/using/auto-configuration.html#using.auto-configuration), [component scan](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html), and be able to define an extra configuration on their "application class". Program entry point – main() : The [main()](basic-syntax.html#program-entry-point) function is the entry point to the application. : It is declared as a [top-level function](functions.html#function-scope) outside the `DemoApplication` class. The `main()` function invokes the Spring's `runApplication(*args)` function to start the application with the Spring Framework. Variable arguments – args: Array : If you check the declaration of the `runApplication()` function, you will see that the parameter of the function is marked with [vararg modifier](functions.html#variable-number-of-arguments-varargs): `vararg args: String`. This means that you can pass a variable number of String arguments to the function. The spread operator – (*args) : The `args` is a parameter to the `main()` function declared as an array of Strings. Since there is an array of strings, and you want to pass its content to the function, use the spread operator (prefix the array with a star sign `*`). ## Create a controller The application is ready to run, but let's update its logic first. In the Spring application, a controller is used to handle the web requests. In the same package, next to the `DemoApplication.kt` file, create the `MessageController.kt` file with the `MessageController` class as follows: ```KOTLIN // MessageController.kt package com.example.demo import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController @RestController class MessageController { @GetMapping("/") fun index(@RequestParam("name") name: String) = "Hello, $name!" } ``` @RestController annotation : You need to tell Spring that `MessageController` is a REST Controller, so you should mark it with the `@RestController` annotation. : This annotation means this class will be picked up by the component scan because it's in the same package as our `DemoApplication` class. @GetMapping annotation : `@GetMapping` marks the functions of the REST controller that implement the endpoints corresponding to HTTP GET calls: : ```KOTLIN @GetMapping("/") fun index(@RequestParam("name") name: String) = "Hello, $name!" ``` @RequestParam annotation : The function parameter `name` is marked with `@RequestParam` annotation. This annotation indicates that a method parameter should be bound to a web request parameter. : Hence, if you access the application at the root and supply a request parameter called "name", like `/?name=`, the parameter value will be used as an argument for invoking the `index()` function. Single-expression functions – index() : Since the `index()` function contains only one statement you can declare it as a [single-expression function](functions.html#single-expression-functions). : This means the curly braces can be omitted and the body is specified after the equals sign `=`. Type inference for function return types : The `index()` function does not declare the return type explicitly. Instead, the compiler infers the return type by looking at the result of the statement on the right-hand side from the equals sign `=`. : The type of `Hello, $name!` expression is `String`, hence the return type of the function is also `String`. String templates – $name : `Hello, $name!` expression is called a [String template](strings.html#string-templates) in Kotlin. : String templates are String literals that contain embedded expressions. : This is a convenient replacement for String concatenation operations. ## Run the application The Spring application is now ready to run: 1. In the `DemoApplication.kt` file, click the green Run icon in the gutter beside the `main()` method: ![Run Spring Boot application](images/run-spring-boot-application.png) Tip: You can also run the `./gradlew bootRun` command in the terminal. This starts the local server on your computer. 2. Once the application starts, open the following URL: ```TEXT http://localhost:8080?name=John ``` You should see "Hello, John!" printed as a response: ![Spring Application response](images/spring-application-response.png) ## Next step In the next part of the tutorial, you'll learn about Kotlin data classes and how you can use them in your application. * [Next step](jvm-spring-boot-add-data-class.html)