by Kevin Peters

This blog post will show how to mix Java and Kotlin within one Spring Boot Web Application. We will use Kotlin data classes side by side with Lombok backed Java POJOs, for example as value objects or Jackson marshalling purposes.

Since Kotlin is supported by Spring Boot 2.0 you can now use both its power and its “non-verbosity” to write an entire application. That sounds really nice if you think about new projects written from scratch, but what about existing projects? Is Lombok the only way to avoid writing getters and setters?

The answer is: “No, it’s not!”. It’s pretty easy to enhance existing “pure Java” applications with Kotlin and use its power to write less code.

A common use case for adding Kotlin to existing projects would probably be to replace Java POJOs used for payload (un-)marshalling. Hence no one wants to write accessors and mutators by hand Lombok is the way to go so far. In the end it always results in something like that:

import lombok.Data ; @Data public class JavaValueObject { private String value = "default" ; } import lombok.Data; @Data public class JavaValueObject { private String value = "default"; }

Indeed, that’s already not that much code, but is there an alternative? There is! A simple Kotlin data class could look like the following:

data class KotlinDataClass ( var value : String = "default" ) data class KotlinDataClass ( var value : String = "default" )

Now you could use both classes within a controller, even side by side:

@RestController public class MixedController { ... @RequestMapping ( path = Binding . PATH_WITH_JAVA_MAPPING , method = RequestMethod. POST ) public JavaValueObject javaMappingEndpoint ( final JavaValueObject input ) { ... } @RequestMapping ( path = Binding . PATH_WITH_KOTLIN_MAPPING , method = RequestMethod. POST ) public KotlinDataClass kotlinMappingEndpoint ( final KotlinDataClass input ) { ... } } @RestController public class MixedController { ... @RequestMapping(path = Binding.PATH_WITH_JAVA_MAPPING, method = RequestMethod.POST) public JavaValueObject javaMappingEndpoint(final JavaValueObject input) { ... } @RequestMapping(path = Binding.PATH_WITH_KOTLIN_MAPPING, method = RequestMethod.POST) public KotlinDataClass kotlinMappingEndpoint(final KotlinDataClass input) { ... } }

To use Kotlin within an exising Spring Boot Application (at the time this article is written Spring Boot 1.5.6 is the most current release version) with Eclipse / STS as IDE and Maven as build tool follow this steps:

1. Install the “Kotlin Plugin for Eclipse” via Marketplace (restart needed afterwards)

2. Add Kotlin nature to your project (PROJECT CONTEXT MENU (right click) -> CONFIGURE KOTLIN -> ADD KOTLIN NATURE)

3. Add following dependencies:

<dependency > <groupId > org.jetbrains.kotlin </groupId > <artifactId > kotlin-stdlib-jre8 </artifactId > <version > ${kotlin.version} </version > </dependency > <dependency > <groupId > com.fasterxml.jackson.module </groupId > <artifactId > jackson-module-kotlin </artifactId > </dependency > <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jre8</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-kotlin</artifactId> </dependency>

4. Add the kotlin-maven-plugin:

<plugin > <artifactId > kotlin-maven-plugin </artifactId > <groupId > org.jetbrains.kotlin </groupId > <version > ${kotlin.version} </version > <configuration > <compilerPlugins > <plugin > spring </plugin > </compilerPlugins > <jvmTarget > ${java.version} </jvmTarget > </configuration > <executions > <execution > <id > compile </id > <phase > compile </phase > <goals > <goal > compile </goal > </goals > <configuration > <sourceDirs > <sourceDir > src/main/kotlin </sourceDir > </sourceDirs > </configuration > </execution > <execution > <id > test-compile </id > <phase > test-compile </phase > <goals > <goal > test-compile </goal > </goals > </execution > </executions > <dependencies > <dependency > <groupId > org.jetbrains.kotlin </groupId > <artifactId > kotlin-maven-allopen </artifactId > <version > ${kotlin.version} </version > </dependency > </dependencies > </plugin > <plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.version}</version> <configuration> <compilerPlugins> <plugin>spring</plugin> </compilerPlugins> <jvmTarget>${java.version}</jvmTarget> </configuration> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> <configuration> <sourceDirs> <sourceDir>src/main/kotlin</sourceDir> </sourceDirs> </configuration> </execution> <execution> <id>test-compile</id> <phase>test-compile</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-allopen</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies> </plugin>

Possibly adjust the path to your Kotlin source folder if it differs to “src/main/kotlin”.

A demo project is available on github.