@ -20,6 +20,9 @@ Feel free to join the #spring channel of https://slack.kotlinlang.org/[Kotlin Sl
@@ -20,6 +20,9 @@ Feel free to join the #spring channel of https://slack.kotlinlang.org/[Kotlin Sl
or ask a question with `spring` and `kotlin` as tags on
https://stackoverflow.com/questions/tagged/spring+kotlin[Stackoverflow] if you need support.
[[kotlin-requirements]]
== Requirements
@ -37,6 +40,9 @@ for serializing or deserializing JSON data for Kotlin classes with Jackson, so m
@@ -37,6 +40,9 @@ for serializing or deserializing JSON data for Kotlin classes with Jackson, so m
`com.fasterxml.jackson.module:jackson-module-kotlin` dependency to your project if you have such need.
It is automatically registered when found in the classpath.
[[kotlin-extensions]]
== Extensions
@ -80,6 +86,9 @@ With Kotlin and the Spring Framework extensions, you can instead write the follo
@@ -80,6 +86,9 @@ With Kotlin and the Spring Framework extensions, you can instead write the follo
As in Java, `users` in Kotlin is strongly typed, but Kotlin's clever type inference allows
for shorter syntax.
[[kotlin-null-safety]]
== Null-safety
@ -115,6 +124,9 @@ NOTE: Generic type arguments, varargs, and array elements nullability are not su
@@ -115,6 +124,9 @@ NOTE: Generic type arguments, varargs, and array elements nullability are not su
but should be in an upcoming release. See https://github.com/Kotlin/KEEP/issues/79[this discussion]
for up-to-date information.
[[kotlin-classes-interfaces]]
== Classes and Interfaces
@ -124,12 +136,16 @@ with default values.
@@ -124,12 +136,16 @@ with default values.
Kotlin parameter names are recognized through a dedicated `KotlinReflectionParameterNameDiscoverer`,
which allows finding interface method parameter names without requiring the Java 8 `-parameters`
compiler flag to be enabled during compilation.
compiler flag to be enabled during compilation. (For completeness, we nevertheless recommend
running the Kotlin compiler with its `-java-parameters` flag for standard Java parameter exposure.)
You can declare configuration classes as
https://kotlinlang.org/docs/reference/nested-classes.html[top level or nested but not inner],
since the later requires a reference to the outer class.
@ -263,16 +282,20 @@ as the following example shows:
@@ -263,16 +282,20 @@ as the following example shows:
}
----
NOTE: Spring Boot is based on JavaConfig and
https://github.com/spring-projects/spring-boot/issues/8115[does not yet provide specific support for functional bean definition],
but you can experimentally use functional bean definitions through Spring Boot's `ApplicationContextInitializer` support.
See https://stackoverflow.com/questions/45935931/how-to-use-functional-bean-definition-kotlin-dsl-with-spring-boot-and-spring-w/46033685#46033685[this Stack Overflow answer]
for more details and up-to-date information. See also the experimental Kofu DSL developed in https://github.com/spring-projects/spring-fu[Spring Fu incubator].
[[kotlin-web]]
== Web
=== Router DSL
Spring Framework comes with a Kotlin router DSL available in 3 flavors:
@ -314,6 +337,8 @@ when you need to register routes depending on dynamic data (for example, from a
@@ -314,6 +337,8 @@ when you need to register routes depending on dynamic data (for example, from a
See https://github.com/mixitconf/mixit/[MiXiT project] for a concrete example.
=== MockMvc DSL
A Kotlin DSL is provided via `MockMvc` Kotlin extensions in order to provide a more
Configuration is usually done with `ScriptTemplateConfigurer` and `ScriptTemplateViewResolver`
beans.
Configuration is usually done with `ScriptTemplateConfigurer` and `ScriptTemplateViewResolver` beans.
`KotlinScriptConfiguration.kt`
[source,kotlin,indent=0]
@ -386,6 +411,8 @@ class KotlinScriptConfiguration {
@@ -386,6 +411,8 @@ class KotlinScriptConfiguration {
See the https://github.com/sdeleuze/kotlin-script-templating[kotlin-script-templating] example
project for more details.
=== Kotlin multiplatform serialization
As of Spring Framework 5.3, https://github.com/Kotlin/kotlinx.serialization[Kotlin multiplatform serialization] is
@ -397,6 +424,9 @@ Kotlin serialization is designed to serialize only Kotlin classes annotated with
@@ -397,6 +424,9 @@ Kotlin serialization is designed to serialize only Kotlin classes annotated with
With Spring Messaging (RSocket), make sure that neither Jackson, GSON or JSONB are in the classpath if you want automatic configuration,
if Jackson is needed configure `KotlinSerializationJsonMessageConverter` manually.
== Coroutines
Kotlin https://kotlinlang.org/docs/reference/coroutines-overview.html[Coroutines] are Kotlin
@ -415,6 +445,8 @@ Spring Framework provides support for Coroutines on the following scope:
@@ -415,6 +445,8 @@ Spring Framework provides support for Coroutines on the following scope:
* Suspending function and `Flow` support in RSocket `@MessageMapping` annotated methods
* Extensions for {docs-spring-framework}/kdoc-api/spring-messaging/org.springframework.messaging.rsocket/index.html[`RSocketRequester`]
=== Dependencies
Coroutines support is enabled when `kotlinx-coroutines-core` and `kotlinx-coroutines-reactor`
Read this blog post about https://spring.io/blog/2019/04/12/going-reactive-with-spring-coroutines-and-kotlin-flow[Going Reactive with Spring, Coroutines and Kotlin Flow]
for more details, including how to run code concurrently with Coroutines.
=== Controllers
Here is an example of a Coroutines `@RestController`.
@ -554,6 +590,8 @@ class CoroutinesViewController(banner: Banner) {
@@ -554,6 +590,8 @@ class CoroutinesViewController(banner: Banner) {
}
----
=== WebFlux.fn
Here is an example of Coroutines router defined via the {docs-spring-framework}/kdoc-api/spring-webflux/org.springframework.web.reactive.function.server/co-router.html[coRouter { }] DSL and related handlers.
@ -587,6 +625,8 @@ class UserHandler(builder: WebClient.Builder) {
@@ -587,6 +625,8 @@ class UserHandler(builder: WebClient.Builder) {
}
----
=== Transactions
Transactions on Coroutines are supported via the programmatic variant of the Reactive
@ -636,6 +676,8 @@ For Kotlin `Flow`, a `Flow<T>.transactional` extension is provided.
@@ -636,6 +676,8 @@ For Kotlin `Flow`, a `Flow<T>.transactional` extension is provided.
----
[[kotlin-spring-projects-in-kotlin]]
== Spring Projects in Kotlin
@ -683,6 +725,8 @@ NOTE: The Kotlin code samples in Spring Framework documentation do not explicitl
@@ -683,6 +725,8 @@ NOTE: The Kotlin code samples in Spring Framework documentation do not explicitl
`open` on the classes and their member functions. The samples are written for projects
using the `kotlin-allopen` plugin, since this is the most commonly used setup.
=== Using Immutable Class Instances for Persistence
In Kotlin, it is convenient and considered to be a best practice to declare read-only properties
@ -726,6 +770,8 @@ NOTE: As of the Kay release train, Spring Data supports Kotlin immutable class i
@@ -726,6 +770,8 @@ NOTE: As of the Kay release train, Spring Data supports Kotlin immutable class i
does not require the `kotlin-noarg` plugin if the module uses Spring Data object mappings
(such as MongoDB, Redis, Cassandra, and others).
=== Injecting Dependencies
Our recommendation is to try to favor constructor injection with `val` read-only (and
@ -761,6 +807,8 @@ as the following example shows:
@@ -761,6 +807,8 @@ as the following example shows:
}
----
=== Injecting Configuration Properties
In Java, you can inject configuration properties by using annotations (such as pass:q[`@Value("${property}")`)].
@ -801,6 +849,7 @@ that uses the `${...}` syntax, with configuration beans, as the following exampl
@@ -801,6 +849,7 @@ that uses the `${...}` syntax, with configuration beans, as the following exampl
----
=== Checked Exceptions
Java and https://kotlinlang.org/docs/reference/exceptions.html[Kotlin exception handling]
@ -813,6 +862,8 @@ To get the original exception thrown like in Java, methods should be annotated w
@@ -813,6 +862,8 @@ To get the original exception thrown like in Java, methods should be annotated w
to specify explicitly the checked exceptions thrown (for example `@Throws(IOException::class)`).
=== Annotation Array Attributes
Kotlin annotations are mostly similar to Java annotations, but array attributes (which are
@ -857,6 +908,8 @@ use a shortcut annotation, such as `@GetMapping`, `@PostMapping`, and others.
@@ -857,6 +908,8 @@ use a shortcut annotation, such as `@GetMapping`, `@PostMapping`, and others.
NOTE: If the `@RequestMapping` `method` attribute is not specified, all HTTP methods will
be matched, not only the `GET` method.
=== Testing
This section addresses testing with the combination of Kotlin and Spring Framework.
@ -866,6 +919,7 @@ https://mockk.io/[Mockk] for mocking.
@@ -866,6 +919,7 @@ https://mockk.io/[Mockk] for mocking.
NOTE: If you are using Spring Boot, see
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-kotlin-testing[this related documentation].
==== Constructor injection
As described in the <<testing#testcontext-junit-jupiter-di, dedicated section>>,
@ -887,6 +941,7 @@ class OrderServiceIntegrationTests(val orderService: OrderService,
@@ -887,6 +941,7 @@ class OrderServiceIntegrationTests(val orderService: OrderService,
----
====
==== `PER_CLASS` Lifecycle
Kotlin lets you specify meaningful test function names between backticks (```).
@ -930,6 +985,7 @@ class IntegrationTests {
@@ -930,6 +985,7 @@ class IntegrationTests {
}
----
==== Specification-like Tests
You can create specification-like tests with JUnit 5 and Kotlin.
@ -959,6 +1015,7 @@ class SpecificationLikeTests {
@@ -959,6 +1015,7 @@ class SpecificationLikeTests {
}
----
[[kotlin-webtestclient-issue]]
==== `WebTestClient` Type Inference Issue in Kotlin
@ -968,17 +1025,24 @@ since it provides a workaround for the Kotlin issue with the Java API.
@@ -968,17 +1025,24 @@ since it provides a workaround for the Kotlin issue with the Java API.
See also the related https://jira.spring.io/browse/SPR-16057[SPR-16057] issue.
[[kotlin-getting-started]]
== Getting Started
The easiest way to learn how to build a Spring application with Kotlin is to follow
For other use cases, especially if you are using blocking technologies such as JPA, Spring
MVC and its annotation-based programming model is the recommended choice.
[[kotlin-resources]]
== Resources
@ -1004,6 +1071,8 @@ Kotlin and the Spring Framework:
@@ -1004,6 +1071,8 @@ Kotlin and the Spring Framework:
* https://blog.jetbrains.com/kotlin/[Kotlin blog]
* https://kotlin.link/[Awesome Kotlin]
=== Examples
The following Github projects offer examples that you can learn from and possibly even extend:
@ -1016,6 +1085,8 @@ The following Github projects offer examples that you can learn from and possibl
@@ -1016,6 +1085,8 @@ The following Github projects offer examples that you can learn from and possibl
* https://github.com/sdeleuze/spring-kotlin-deepdive[spring-kotlin-deepdive]: A step-by-step migration guide for Boot 1.0 and Java to Boot 2.0 and Kotlin
* https://github.com/spring-cloud/spring-cloud-gcp/tree/master/spring-cloud-gcp-kotlin-samples/spring-cloud-gcp-kotlin-app-sample[spring-cloud-gcp-kotlin-app-sample]: Spring Boot with Google Cloud Platform Integrations
=== Issues
The following list categorizes the pending issues related to Spring and Kotlin support: