@@ -7534,6 +7534,225 @@ Boot's core features will be honoured by the presence of the core starter.
75347534
75357535
75367536
7537+ [[boot-features-kotlin]]
7538+ == Kotlin support
7539+ https://kotlinlang.org[Kotlin] is a statically-typed language targeting the JVM (and other
7540+ platforms) which allows writing concise and elegant code while providing
7541+ {kotlin-documentation}java-interop.html[interoperability] with existing libraries written
7542+ in Java.
7543+
7544+ Spring Boot provides Kotlin support by leveraging the support in other Spring projects
7545+ such as Spring Framework, Spring Data, and Reactor. See the
7546+ {spring-reference}languages.html#kotlin[Spring Framework Kotlin support documentation]
7547+ for more information.
7548+
7549+ The easiest way to start with Spring Boot and Kotlin is to create a project via
7550+ https://start.spring.io/#!language=kotlin[start.spring.io]. Feel free to join the #spring
7551+ channel of http://slack.kotlinlang.org/[Kotlin Slack] or ask a question with `spring`
7552+ and `kotlin` tags on https://stackoverflow.com/questions/tagged/spring+kotlin[Stack
7553+ Overflow] if you need support.
7554+
7555+
7556+
7557+ [[boot-features-kotlin-requirements]]
7558+ === Requirements
7559+ Spring Boot supports Kotlin 1.2.x. To use Kotlin, `org.jetbrains.kotlin:kotlin-stdlib` and
7560+ `org.jetbrains.kotlin:kotlin-reflect` must be present on the classpath. The
7561+ `kotlin-stdlib` variants `kotlin-stdlib-jdk7` and `kotlin-stdlib-jdk8` can also be used.
7562+
7563+ Since https://discuss.kotlinlang.org/t/classes-final-by-default/166[Kotlin classes are
7564+ final by default], you are likely to want to configure
7565+ {kotlin-documentation}compiler-plugins.html#spring-support[kotlin-spring]
7566+ plugin in order to automatically open Spring-annotated classes so that they can be
7567+ proxied.
7568+
7569+ https://github.com/FasterXML/jackson-module-kotlin[Jackson's Kotlin module] is required
7570+ for serializing / deserializing JSON data in Kotlin. It is automatically registered when
7571+ found on the classpath. A warning message is logged if Jackson and Kotlin are present but
7572+ the Jackson Kotlin module is not.
7573+
7574+ TIP: These dependencies and plugins are provided by default if one bootstraps a Kotlin
7575+ project on https://start.spring.io/#!language=kotlin[start.spring.io].
7576+
7577+
7578+
7579+ [[boot-features-kotlin-null-safety]]
7580+ === Null-safety
7581+ One of Kotlin's key features is {kotlin-documentation}null-safety.html[null-safety]. It
7582+ deals with `null` values at compile time rather than deferring the problem to runtime and
7583+ encountering a `NullPointerException`. This helps to eliminate a common source of bugs
7584+ without paying the cost of wrappers like `Optional`. Kotlin also allows using functional
7585+ constructs with nullable values as described in this
7586+ http://www.baeldung.com/kotlin-null-safety[comprehensive guide to null-safety in Kotlin].
7587+
7588+ Although Java does not allow one to express null-safety in its type system, Spring
7589+ Framework, Spring Data, and Reactor now provide null-safety of their API via
7590+ tooling-friendly annotations. By default, types from Java APIs used in Kotlin are
7591+ recognized as
7592+ {kotlin-documentation}java-interop.html#null-safety-and-platform-types[platform types]
7593+ for which null-checks are relaxed.
7594+ {kotlin-documentation}java-interop.html#jsr-305-support[Kotlin's support for JSR 305
7595+ annotations] combined with nullability annotations provide null-safety for the related
7596+ Spring API in Kotlin.
7597+
7598+ The JSR 305 checks can be configured by adding the `-Xjsr305` compiler flag with the
7599+ following options: `-Xjsr305={strict|warn|ignore}`. The default behavior is the same as
7600+ `-Xjsr305=warn`. The `strict` value is required to have null-safety taken in account in
7601+ Kotlin types inferred from Spring API but should be used with the knowledge that Spring
7602+ API nullability declaration could evolve even between minor releases and more checks may
7603+ be added in the future).
7604+
7605+ WARN: Generic type arguments, varargs and array elements nullability are not yet
7606+ supported. See https://jira.spring.io/browse/SPR-15942[SPR-15942] for up-to-date
7607+ information. Also be aware that Spring Boot's own API is {github-issues}10712[not yet
7608+ annotated].
7609+
7610+
7611+
7612+ [[boot-features-kotlin-api]]
7613+ === Kotlin API
7614+
7615+
7616+
7617+ [[boot-features-kotlin-api-runapplication]]
7618+ ==== runApplication
7619+ Spring Boot provides an idiomatic way to run an application with
7620+ `runApplication<FooApplication>(*args)` as shown in the following example:
7621+
7622+ [source,kotlin,indent=0]
7623+ ----
7624+ import org.springframework.boot.autoconfigure.SpringBootApplication
7625+ import org.springframework.boot.runApplication
7626+
7627+ @SpringBootApplication
7628+ class FooApplication
7629+
7630+ fun main(args: Array<String>) {
7631+ runApplication<FooApplication>(*args)
7632+ }
7633+ ----
7634+
7635+ This is a drop-in replacement for
7636+ `SpringApplication.run(FooApplication::class.java, *args)`. It also allows customization
7637+ of the application as shown in the following example:
7638+
7639+ [source,kotlin,indent=0]
7640+ ----
7641+ runApplication<FooApplication>(*args) {
7642+ setBannerMode(OFF)
7643+ }
7644+ ----
7645+
7646+
7647+
7648+ [[boot-features-kotlin-api-extensions]]
7649+ ==== Extensions
7650+ Kotlin {kotlin-documentation}extensions.html[extensions] provide the ability
7651+ to extend existing classes with additional functionality. The Spring Boot Kotlin API makes
7652+ use of these extensions to add new Kotlin specific conveniences to existing APIs.
7653+
7654+ `TestRestTemplate` extensions, similar to those provided by Spring Framework for
7655+ `RestOperations` in Spring Framework, are provided. Among other things, the extensions
7656+ make it possible to take advantage of Kotlin reified type parameters.
7657+
7658+
7659+
7660+ [[boot-features-kotlin-dependency-management]]
7661+ === Dependency management
7662+ In order to avoid mixing different version of Kotlin dependencies on the classpath,
7663+ dependency management of the following Kotlin dependencies is provided:
7664+
7665+ - `kotlin-reflect`
7666+ - `kotlin-runtime`
7667+ - `kotlin-stdlib`
7668+ - `kotlin-stdlib-jdk7`
7669+ - `kotlin-stdlib-jdk8`
7670+ - `kotlin-stdlib-jre7`
7671+ - `kotlin-stdlib-jre8`
7672+
7673+ With Maven, the Kotlin version can be customized via the `kotlin.version` property and
7674+ plugin management is provided for `kotlin-maven-plugin`. With Gradle, the Spring Boot
7675+ plugin automatically aligns the `kotlin.version` with the version of the Kotlin plugin.
7676+
7677+
7678+
7679+ [[boot-features-kotlin-configuration-properties]]
7680+ === `@ConfigurationProperties`
7681+ `@ConfigurationProperties` currently only works with `lateinit` or nullable `var`
7682+ properties (the former is recommended), since immutable classes initialized by
7683+ constructors are {github-issues}8762[not yet supported].
7684+
7685+ [source,kotlin,indent=0]
7686+ ----
7687+ @ConfigurationProperties("example.kotlin")
7688+ class KotlinExampleProperties {
7689+
7690+ lateinit var foo1: String
7691+
7692+ lateinit var foo2: String
7693+
7694+ lateinit val bar = Bar()
7695+
7696+ class Bar {
7697+
7698+ lateinit var bar1: String
7699+
7700+ lateinit var bar2: String
7701+
7702+ }
7703+
7704+ }
7705+ ----
7706+
7707+
7708+ [[boot-features-kotlin-testing]]
7709+ === Testing
7710+ While it is possible to use JUnit 4 (the default provided by `spring-boot-starter-test`)
7711+ to test Kotlin code, JUnit 5 is recommended. JUnit 5 enables a test class to be
7712+ instantiated once and reused for all of the class's tests. This makes it possible to use
7713+ `@BeforeAll` and `@AfterAll` annotations on non-static methods, which is a good fit for
7714+ Kotlin.
7715+
7716+ To use JUnit 5, exclude `junit:junit` dependency from `spring-boot-starter-test`, add
7717+ JUnit 5 dependencies, and configure the Maven or Gradle plugin accordingly. See the
7718+ {junit5-documentation}/#dependency-metadata-junit-jupiter-samples[JUnit 5
7719+ documentation] for more details. You also need to
7720+ {junit5-documentation}/#writing-tests-test-instance-lifecycle-changing-default[switch test
7721+ instance lifecycle to "per-class"].
7722+
7723+
7724+
7725+ [[boot-features-kotlin-resources]]
7726+ === Resources
7727+
7728+
7729+
7730+ [[boot-features-kotlin-resources-further-reading]]
7731+ ==== Further reading
7732+ * {kotlin-documentation}[Kotlin language reference]
7733+ * http://slack.kotlinlang.org/[Kotlin Slack] (with a dedicated #spring channel)
7734+ * https://stackoverflow.com/questions/tagged/spring+kotlin[Stackoverflow with `spring` and `kotlin` tags]
7735+ * https://try.kotlinlang.org/[Try Kotlin in your browser]
7736+ * https://blog.jetbrains.com/kotlin/[Kotlin blog]
7737+ * https://kotlin.link/[Awesome Kotlin]
7738+ * https://spring.io/blog/2016/02/15/developing-spring-boot-applications-with-kotlin[Developing Spring Boot applications with Kotlin]
7739+ * https://spring.io/blog/2016/03/20/a-geospatial-messenger-with-kotlin-spring-boot-and-postgresql[A Geospatial Messenger with Kotlin, Spring Boot and PostgreSQL]
7740+ * https://spring.io/blog/2017/01/04/introducing-kotlin-support-in-spring-framework-5-0[Introducing Kotlin support in Spring Framework 5.0]
7741+ * https://spring.io/blog/2017/08/01/spring-framework-5-kotlin-apis-the-functional-way[Spring Framework 5 Kotlin APIs, the functional way]
7742+
7743+
7744+
7745+ [[boot-features-kotlin-resources-examples]]
7746+ ==== Examples
7747+
7748+ * https://github.com/sdeleuze/spring-boot-kotlin-demo[spring-boot-kotlin-demo]: regular Spring Boot + Spring Data JPA project
7749+ * https://github.com/mixitconf/mixit[mixit]: Spring Boot 2 + WebFlux + Reactive Spring Data MongoDB
7750+ * https://github.com/sdeleuze/spring-kotlin-fullstack[spring-kotlin-fullstack]: WebFlux Kotlin fullstack example with Kotlin2js for frontend instead of JavaScript or TypeScript
7751+ * https://github.com/spring-petclinic/spring-petclinic-kotlin[spring-petclinic-kotlin]: Kotlin version of the Spring PetClinic Sample Application
7752+ * https://github.com/sdeleuze/spring-kotlin-deepdive[spring-kotlin-deepdive]: a step by step migration for Boot 1.0 + Java to Boot 2.0 + Kotlin
7753+
7754+
7755+
75377756[[boot-features-whats-next]]
75387757== What to Read Next
75397758If you want to learn more about any of the classes discussed in this section, you can
0 commit comments