Let’s create web application with Kotlin and Spring. Spring is really sophisticated Java framework, and can do almost of all everything.
This time, I use kotlinx.html for view template. (Thymeleaf also can be used.) In kotlinx.html, we can write everything in Kotlin grammar. For project structure management, I use Gradle. Intellij IDEA Community Edition for editor.
Environment
- Gradle 4.3.1 (In the middle of article, I upgrade Gradle version to 4.4 RC 1)
- Kotlin 1.1.61
- kotlinx.html 0.6.6
- Spring Boot 1.5.8
- PostgreSQL 9.5.6
- Intellij IDEA 2017.2.6 (Community Edition)
- OpenJDK 1.8
Build base of the application
Generate base with Intellij IDEA wizard. ファイルメニューから 新しいプロジェクトを作成します。 “New Project” dialog will be shown, then choose Gradle in left pain, check “Kotlin (Java)”, click “Next”.
Set GroupId, ArtifactId to com.example, myapp respectively.
Then, build.gradle
is as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
group 'com.example' version '1.0-SNAPSHOT' buildscript { ext.kotlin_version = '1.1.61' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } apply plugin: 'kotlin' repositories { mavenCentral() } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" } compileKotlin { kotlinOptions.jvmTarget = "1.8" } compileTestKotlin { kotlinOptions.jvmTarget = "1.8" } |
build.gradle
の dependencies
に compile("org.springframework.boot:spring-boot-starter-web:1.5.8.RELEASE")
を追加します。
Create Spring Boot main method.
1 2 3 4 5 |
src - main - kotlin - com.example.myapp - Application.kt |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.example.myapp import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.web.support.SpringBootServletInitializer @SpringBootApplication open class Application: SpringBootServletInitializer() { companion object { @JvmStatic fun main(args: Array<String>) { SpringApplication.run(Application::class.java, *args) } } } |
Now, server will work.。
Continue to Kotlin 1.1.61 + Spring で Web Application を作る (2 of 4).