Skip to main content

Spring Boot Kotlin - Bootstrapping

This chapter will explain you how to perform bootstrapping on a Spring Boot application.

Spring Initializer

  • There are many ways to create a Project Spring Boot, but in this tutorial we will take you to create a Project Spring Boot on the Spring Initializer website.

Spring Initializer Dashboard

  • In the image above, you will see a dashboard that is used to create a Project Spring Boot. We will now take you through each point in this dashboard

    • Project: In this section, you are required to select the type of Project Manager (Project Manager is the one who can properly organize the dependencies that you need to use in the project, and on the other hand can set the version of the project and more, etc.) And for project managers, there are two as follows:

      • Maven is a project management tool that is open-source software and is primarily used for Java Project.
      • Gradle is an open-source automation tool based on the concepts of Apache Maven and Apache Ant.

      The reason you should use Gradle is because it avoids tracking the same tasks and it only tracks the tasks that have been changed, so it provides faster processing.

    • Language: In this section you are required to select the following language types:

      • Java is a widely used programming language running on millions of devices including PCs, laptops, mobile devices, gaming consoles, medical devices and more. The synctax of the Java language is dependent on the C and C++ languages.
      • Kotlin is a new open source programming language similar to Java and JavaScript. It is a rich language that brings together many functions in one place. Kotlin currently runs on JVMs.
      • Groovy is object oriented and has a Java-like format. Groovy code is compiled into Java Bytecode and can run on any platform with JRE.
    • Spring Boot: In this section you are required to select the version of Spring Boot, so you should choose the latest version.

    • Project Metadata: In this section you are required to fill in some project related information:

      • Group specifies the name of the package.
      • Artifact is the name of the Application.
      • Name same as Artifact.
      • Description as a description of the Project.
      • Package name same as Group.
      • Packaging is a selection of types of packaging for build to take to use, and there are two:
        • Jar is a Java Application Archive that can run programs on your computer.
        • War is a Web Application Archive that can only run programs on the server.
      • Java is a selection of versions of Java, so you can choose which version you want to use.
    • Dependencies: In this section, you are required to select a type of dependency, so you can select the dependencies as shown in the picture or select only one Spring Web.

    After this you will see three more buttons below:

    • GENERATE: a button for creating a Spring Boot Project.
    • EXPLOER: a button to view the Structure of Spring Boot Project.
    • SHARE...: a button for sharing Spring Boot Project.
  • After clicking the Generate button, it provides a zip file as shown below:

Zip File Project

  • Then you can extract the zip file, then open the IntelliJ IDEA IDE and open the project you just extracted.

IntelliJ

  • After opening the project in the IntelliJ IDEA IDE, you have to wait for the IntelliJ IDEA IDE to sync the project completely before you can start developing this project.

Start Project Spring IntelliJ

  • After the project is synced, you can take a look at the project structure as shown below:

Spring Boot Starter Folder

  • The image below is a project structure that was extracted from a zip file and has not been properly configured.

Spring Boot Before Delete Unused File

  • The image below is a project structure that deletes useless folders or files and organizes them properly.

Spring Boot After Delete Unused File

  • The image below is the build.gradle.kts file, where you can add additional dependencies or modify versions of Java, Kotlin, Spring Boot, etc.

Spring Boot Grandle Build

  • Find out more about build.gradle.kts file:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("org.springframework.boot") version "2.5.1"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.5.10"
kotlin("plugin.spring") version "1.5.10"
kotlin("plugin.jpa") version "1.5.10"
}
  • Plugins: In addition to the Kotlin Gradle plugin, the default configuration is the kotlin-spring-plugin declaration, which allows the classes and methods to be open, allowing other classes to be inherited(classes in Kotlin are not As in Java, if you do not put the open keyword in front of the class, it will be the final class). On the other hand, to be able to use Kotlin non-nullable properties with JPA, you need to have the Kotlin JPA plugin enabled. This will create no-arg constructors for all classes annotated with @Entity, @MappedSuperclass, or @Embeddeable.
group = "com.webapp"
version = "0.0.1-SNAPSHOT"
java.soruceCompatibility = JavaVersion.VERSION_11
  • Above is a group that is the package name, version is the version of the application that was built, java.soruceCompatibility is the setting for the version of Java for build.
repositories {
mavenCentral()
}
  • Repositories: Set the location for downloading dependencies to use in the Project, as when you put mavenCentral(), it will go to the Maven Central Repository download site.
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
  • Dependencies: The three Kotlin libraries required for Project Spring Boot are as follows:

    • kotlin-stdlib-jdk8: The Kotlin library standard of the Java 8 Variant.
    • kotlin-reflect: A Kotlin reflection library.
    • jackson-module-kotlin: Provides support for serialization/deserialization of Kotlin classes and data classes.

    And there are some dependencies that belong to Spring and Database as follows:

    • spring-boot-starter-data-jpa: A Spring Data JPA library.
    • spring-boot-starter-web: A Spring Web library.
    • h2: The library of the H2 Database.
    • spring-boot-starter-test: Spring Test library.

    On the other hand, in every download dependencies, there is the use of different functions, such as implementation used to run those dependencies, starting from the implementation (when coding), runtimeOnly is only run at run time and the testImplementation is run during the test.

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
  • Compile options: The main feature of Kotlin is null-safety, as it solves a lot of problems when compiling time, it avoids the famous exception NullPointerException at run time. To add this null-safety function, you need to add -Xjsr305 compiler with flag with strict.
tasks.withType<Test> {
useJUnitPlatform()
}
  • Test options: A registration for testing using JUnit5.

  • The following image is in the application.properties file, but we have modified the file extension to yml, and in this file we need to write a configuration with H2 Database to be able to connect to H2 Database.

Spring Boot Config H2 Database

  • The image below is the file that is the Main Application in Spring Boot and when you press the button to execute the program.

Spring Boot Application Starter

  • The image below is the Console Logger where the program is executed.

Spring Boot Starter 8090

  • The image below is when the program is successfully executed, then go to view the results on the browser. The reason it displays the word Whitelabel Error Page is because you have not yet created a Controller for this page.

Spring Boot Starter 8090 Result

  • The image below will take you to create a file called TestController.

Test Contoller Folder

  • In the TestController file, there will be a path creation for the '/testing-page' and a Get function.

Test Contoller File

  • After creating a controller and executing the program again, and after successful execution, go to the results on the '/testing-page' will see the following results:

Test Contoller File Result

Summary

  • After studying this chapter, you will be able to create your own Project Spring Boot.