<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vincent Poilvert on Medium]]></title>
        <description><![CDATA[Stories by Vincent Poilvert on Medium]]></description>
        <link>https://medium.com/@v.poilvert?source=rss-b248106e5f54------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Vincent Poilvert on Medium</title>
            <link>https://medium.com/@v.poilvert?source=rss-b248106e5f54------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 19:47:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@v.poilvert/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[My first Spring Boot 2 application with Kotlin]]></title>
            <link>https://medium.com/zenika/my-first-spring-boot-2-application-with-kotlin-66ee97b7366b?source=rss-b248106e5f54------2</link>
            <guid isPermaLink="false">https://medium.com/p/66ee97b7366b</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Vincent Poilvert]]></dc:creator>
            <pubDate>Tue, 15 May 2018 07:07:30 GMT</pubDate>
            <atom:updated>2018-05-15T14:54:23.934Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*J88jEbJzlZ7pKKOP." /></figure><p><a href="https://kotlinlang.org/">Kotlin</a> is a programming language developed by JetBrains (IntelliJ IDEA) and presented as a replacement for Java. As a Java / Spring developer for a few years I was curious to see what the language had to offer. Since Spring Boot 2 was released March 1st, I’ve decided to develop my first Kotlin web application.</p><p>The goal is to create a book management app “kbooks” using the following libraries :</p><ul><li>Spring 5</li><li>Hibernate (with JPA)</li><li>Jersey</li><li>H2</li></ul><p>The complete project is available on GitHub : <a href="https://github.com/Zenika/kbooks/tree/1.0.0">https://github.com/Zenika/kbooks/tree/1.0.0</a></p><h3>Prerequisites</h3><p>If you are not familiar with Kotlin syntax I invite you to follow this link that shows the language basics : <a href="https://medium.com/@magnus.chatt/why-you-should-totally-switch-to-kotlin-c7bbde9e10d5">https://medium.com/@magnus.chatt/why-you-should-totally-switch-to-kotlin-c7bbde9e10d5</a></p><h3>Project Creation</h3><p>The project creation is done in the same way as Java using <a href="http://start.spring.io/">http://start.spring.io/</a>, I select Kotlin in the language list. Then I add the dependencies I need.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*p735kscmHBhVWbpC." /></figure><h3>Model</h3><p>To create the entities I use Kotlin <a href="https://kotlinlang.org/docs/reference/data-classes.html">data classes</a>. All the parameters must have a default value to have the Kotlin compiler generate an empty constructor, mandatory with Hibernate. Notice the usage of <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-list-of.html">mutableListOf()</a> method to create an empty mutable list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*i9nDIy17NgGc8q26." /><figcaption><a href="https://github.com/Zenika/kbooks/blob/1.0.0/src/main/kotlin/com/zenika/kbooks/feature/book/Book.kt">Book.kt</a>, <a href="https://github.com/Zenika/kbooks/blob/1.0.0/src/main/kotlin/com/zenika/kbooks/feature/author/Author.kt">Author.kt</a></figcaption></figure><p>Then I create the repository interfaces. They inherit from PagingAndSortingRepository interface and their implementation will be generated by Spring Data. Since these interfaces have an empty body, I can omit the braces.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2OyV6H5VWa_qZBOp." /><figcaption><a href="https://github.com/Zenika/kbooks/blob/1.0.0/src/main/kotlin/com/zenika/kbooks/feature/book/IBookRepository.kt">IBookRepository.kt</a>, <a href="https://github.com/Zenika/kbooks/blob/1.0.0/src/main/kotlin/com/zenika/kbooks/feature/author/IAuthorRepository.kt">IAuthorRepository.kt</a></figcaption></figure><h3>Book management service</h3><p>Now that I have created the data model and the repository interfaces, I create the book management service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*50Ghet9TNBfpOPf9." /><figcaption><a href="https://github.com/Zenika/kbooks/blob/1.0.0/src/main/kotlin/com/zenika/kbooks/feature/book/BookService.kt">BookService.kt</a></figcaption></figure><p>In this example I use some Kotlin features :</p><ul><li>The <a href="https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties-and-variables">lateinit</a> keyword that allows dependency injection for non null types</li><li><a href="https://kotlinlang.org/docs/reference/idioms.html#string-interpolation">it</a> the name of the default parameter for lambdas when there is only one parameter</li><li>The elvis operator <a href="https://kotlinlang.org/docs/reference/null-safety.html#elvis-operator">?:</a> executes its right operand if its left value is null</li><li>String <a href="https://kotlinlang.org/docs/reference/idioms.html#string-interpolation">interpolation</a>. In “Book $id does not exist”, $id will be replaced by the id variable value.</li></ul><p>I also create the BookDto dto using a data class :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JboeDAHyI3kk9GJI." /><figcaption><a href="https://github.com/Zenika/kbooks/blob/1.0.0/src/main/kotlin/com/zenika/kbooks/feature/book/BookDto.kt">BookDto.kt</a></figcaption></figure><p>Finally the entity is converted to a dto by the BookDtoConverter singleton :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8CV6XWQFnQ_EBGkU." /><figcaption><a href="https://github.com/Zenika/kbooks/blob/1.0.0/src/main/kotlin/com/zenika/kbooks/feature/book/BookDtoConverter.kt">BookDtoConverter.kt</a></figcaption></figure><p>It’s the <a href="https://kotlinlang.org/docs/reference/object-declarations.html#object-declarations">object</a> keyword that define BookDtoConverter as a singleton. It’s instance will be created by the Kotlin runtime in a thread safe manner, when the method is called for the first time.</p><h3>The REST layer</h3><p>Now to expose the service with a REST api I use Jax-RS with Jersey.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kc5kXaNMuMnRa5JW." /><figcaption><a href="https://github.com/Zenika/kbooks/blob/1.0.0/src/main/kotlin/com/zenika/kbooks/feature/book/BookResource.kt">BookResource.kt</a></figcaption></figure><p>Since the getBook method content is very short, I use a simple notation without braces.</p><p>Finally, I create a Spring configuration to expose the resource :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ucdhT9FMAPBcmPjC." /><figcaption><a href="https://github.com/Zenika/kbooks/blob/1.0.0/src/main/kotlin/com/zenika/kbooks/config/JerseyConfig.kt">JerseyConfig.kt</a></figcaption></figure><p>This class inherits from ResourceConfig and it’s default constructor calls the ResourceConfig empty constructor. We register the resource in an <a href="https://kotlinlang.org/docs/reference/classes.html#constructors">init</a> block that will be called by all the class constructors (if I add one later).</p><h3>Testing with Mockito-Kotlin</h3><p>I do the tests with JUnit, Mockito, <a href="https://kotlinlang.org/api/latest/kotlin.test/index.html">kotlin-test</a> and <a href="https://github.com/nhaarman/mockito-kotlin">mockito-kotlin</a>. These two last libraries are wrappers of the two first that make writing of unit tests easier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eZZ7Jlbmz5DZJkTD." /><figcaption><a href="https://github.com/Zenika/kbooks/blob/1.0.0/src/test/kotlin/com/zenika/kbooks/feature/book/BookServiceTest.kt">BookServiceTest.kt</a></figcaption></figure><p>These unit tests are similar to the ones written in Java but once again the syntax is more concise.</p><h3>Conclusion</h3><p>The development of a Spring Boot web application with Kotlin is very close to what is done in Java, we do not lose our bearings. Nevertheless these few examples put forward Kotlin advantages, the syntax is more concise and it feels more productive..</p><p>To go further, I invite you to discover the other platforms supported by Kotlin. For example you can use it to write React applications but also Windows or Android native applications. And if you want to test Kotlin without installing it, I invite you to test the <a href="https://github.com/Zenika/alpine-kotlin">alpine-kotlin</a> Docker image made by Zenika :).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=66ee97b7366b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/zenika/my-first-spring-boot-2-application-with-kotlin-66ee97b7366b">My first Spring Boot 2 application with Kotlin</a> was originally published in <a href="https://medium.com/zenika">Zenika</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>