Spring Boot project

creating new project

One way to create a Spring Boot project is to use start.spring.io. Just select what you want, for example gradle and kotlin. Download the project and import it into your IDE.

What is about dependencies? Usually you will need to select at least Spring Web dependency.

// default starter is 
// implementation("org.springframework.boot:spring-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-web")

This allows to execute application with embedded Tomcat Apache container. While application is running, you can see pages in browser by address http://localhost:8080/.

project structure

Typically, code is grouped by the Spring Boot stereotypes and resources are grouped by types. Static resources must be in directory "static" including web pages. But when a template engine is used, views can have other place. For example, for the Thymeleaf template engine default directory is "templates". So even static web pages must be placed there.

kotlin
--com.example.demo
----config
----controllers
----events
----model
------data
------db
------viewmodel
----services
----utils
----DemoApplication.kt

resources
--static
----audio
----css
----images
----js
----libs
--templates
application.properties

note for Kotlin

Kotlin allows you to define functions that are not inside the class. To convert them to static Java methods, the file name of compiled class will have the suffix "Kt". So when you run Spring Boot application from command line, your entry point class must be with suffix "Kt". For example, let you have source code in file DemoApplication.kt

@SpringBootApplication
class DemoApplication 

fun main(args: Array<String>) {
	// SpringApplication.run(DemoApplication::class.java, *args)
	 runApplication<DemoApplication>(*args)
}

Then two files will be generated: DemoApplication.class and DemoApplicationKt.class. And if you run unpacked jar you need point com.example.demo.DemoApplicationKt as main class.

By default, java and kotlin sources stored in different directories: "src/main/java" and "src/main/kotlin" respectively. Previously, this was the only option. But now you can add the kotlin directory as java sources or vice versa. Just add in your gradle file a path.

sourceSets["main"].java.srcDir("src/main/kotlin")
sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}