Beliebte Suchanfragen

Cloud Native

DevOps

IT-Security

Agile Methoden

Java

//

How to mix Java and Kotlin within one Spring Boot Application

12.9.2017 | 2 minutes of reading time

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:

1import lombok.Data;
2 
3@Data
4public class JavaValueObject {
5 
6    private String value = "default";
7 
8}

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:

1data class KotlinDataClass (
2 
3    var value : String = "default"
4 
5)

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

1@RestController
2public class MixedController {
3 
4    ...
5 
6    @RequestMapping(path = Binding.PATH_WITH_JAVA_MAPPING, method = RequestMethod.POST)
7     public JavaValueObject javaMappingEndpoint(final JavaValueObject input) {
8         ...
9     }
10 
11    @RequestMapping(path = Binding.PATH_WITH_KOTLIN_MAPPING, method = RequestMethod.POST)
12     public KotlinDataClass kotlinMappingEndpoint(final KotlinDataClass input) {
13         ...
14     }
15 
16}

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:

1<dependency>
2    <groupId>org.jetbrains.kotlin</groupId>
3    <artifactId>kotlin-stdlib-jre8</artifactId>
4    <version>${kotlin.version}</version>
5</dependency>
6<dependency>
7    <groupId>com.fasterxml.jackson.module</groupId>
8    <artifactId>jackson-module-kotlin</artifactId>
9</dependency>

4. Add the kotlin-maven-plugin :

1<plugin>
2    <artifactId>kotlin-maven-plugin</artifactId>
3    <groupId>org.jetbrains.kotlin</groupId>
4    <version>${kotlin.version}</version>
5    <configuration>
6        <compilerPlugins>
7            <plugin>spring</plugin>
8        </compilerPlugins>
9        <jvmTarget>${java.version}</jvmTarget>
10    </configuration>
11    <executions>
12        <execution>
13            <id>compile</id>
14            <phase>compile</phase>
15            <goals>
16                <goal>compile</goal>
17            </goals>
18            <configuration>
19                <sourceDirs>
20                    <sourceDir>src/main/kotlin</sourceDir>
21                </sourceDirs>
22            </configuration>
23        </execution>
24        <execution>
25            <id>test-compile</id>
26            <phase>test-compile</phase>
27            <goals>
28                <goal>test-compile</goal>
29            </goals>
30        </execution>
31    </executions>
32    <dependencies>
33        <dependency>
34            <groupId>org.jetbrains.kotlin</groupId>
35            <artifactId>kotlin-maven-allopen</artifactId>
36            <version>${kotlin.version}</version>
37        </dependency>
38    </dependencies>
39</plugin>

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

A demo project is available on github .

share post

Likes

3

//

More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.