How to Create a Spring Boot Application using Kotlin and Gradle
A few months ago I wrote an article showing how to create a simple Spring Boot application using Java, you can see it by clicking here.
Now, I am going to show you, how to create the same application using Kotlin and Gradle.
The requirements are:
- Gradle installed;
- Text Editor installed, I recommend Visual Studio Code
Now, you must go to the following website:
Choose to generate a Gradle Project with Kotlin and fill in the group and the artifact fields. Example:
- Group: com.mydomain
- Artifact: demo
In the Search for dependencies field fill in: Web
The options chosen will be as the image below:
Now, click on the Generate Project button
So, download the ZIP file and unzip it.
Using a text editor ( I recommend Visual Studio Code), open the demo folder.
We are going to create the web controller file named HelloWorldController.kt at demo\src\main\kotlin\com\example\demo\\
folder:
Your respective code will be:
Now, using the command prompt you can initialize the application using the command: gradle bootrun
. It is going to use Gradle.
After the execution, you will see the success message referring to the initialization from an embedded Apache Tomcat.
** <==========—> 80% EXECUTING [1m 40s]
:bootRun ** **
So, access http://localhost:8080 in the web browser.
Finally, your first Spring Boot application with Kotlin and Gradle was created.
Thanks for reading.