Simple REST Endpoint with Spring Boot, Kotlin, and Docker

In this tutorial, we’ll create a simple REST controller using Spring Boot with Kotlin and Gradle. We’ll also discover how easily we can deploy it as a Docker image.
A featured image for category: Spring

1. Introduction

In this tutorial, we’ll create a simple REST controller using Spring Boot with Kotlin and Gradle. We’ll also discover how easily we can deploy it as a Docker image.

2. Imports

In order to implement our REST controller with Spring Boot and Kotlin, we’ll have to add the following dependencies to our project:

implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")

(For new projects, we’d recommend creating them using https://start.spring.io)

3. Create a Data Class

As the next step, let’s implement a Person data class consisting of two immutable properties: id and a name:

data class PersonResponse(val id: Int, val name: String)

4. Controller

We can now create a PersonController with getPerson function:

@RestController
class PersonController {

   val personId = AtomicInteger()

   @GetMapping("/person")
   fun getPerson(@RequestParam(value = "name", defaultValue = "Foo") name: String) =
           PersonResponse(personId.incrementAndGet(), name)
}

This is a very simple function responding to GET requests to /person?name={userValue} and returning a JSON object which represents an instance of PersonResponse.

5. Run the Application

Finally, we can run our application using one of the Gradle Wrapper tasks:

./gradlew bootRun

Let’s verify the output:

curl localhost:8080/person?name=John 
curl localhost:8080/person 
{"id":1,"name":"John"}
{"id":2,"name":"Foo"}

As can be seen, the default value “Foo” is used, when we don’t specify any value for the name parameter in our request.

6. Dockerize the Application

So at this point, we have a working REST API created with Spring Boot and Kotlin and the last thing we need is Docker integration.

6.1. Create a Dockerfile

In order to dockerize our application, we will need to create a Dockerfile and put it in the root directory of our project:

FROM openjdk 
VOLUME /tmp
RUN mkdir /work
COPY . /work
WORKDIR /work
RUN /work/gradlew build
RUN mv /work/build/libs/*.jar /work/app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/work/app.jar"]

6.2. Create a .dockerignore

Let’s exclude unnecessary directories and files from the building process:

.gradle
build
out

6.3. Check the Dockerfile

Finally, let’s build the image and create a new container:

docker build --no-cache -t person-app .
docker run -it --rm -p 8080:8080 person-app

Once the process is finished, we can repeat steps from step 5 and ensure that everything is working properly.

7. REST API With Spring Boot, Kotlin and Docker Summary

In this article, we’ve discovered how easy it is to expose a REST endpoint in a containerized Spring Boot application with Kotlin, Gradle, and Docker. If you’d like to see the whole project implementation, please head to our GitHub project.

Finally, if you really enjoyed this post, I’d be very grateful if you’d help it spread by sharing it with your friends. Let’s build a better community together! 🙂

If you have any ideas or comments that you would like to share with us, please let us know in the comments below, by our Facebook page or group, or by using our contact form.

Share this:

Related content

Newsletter
Image presents 3 ebooks with Java, Spring and Kotlin interview questions.

Never miss any important updates from the Kotlin world and get 3 ebooks!

You may opt out any time. Terms of Use and Privacy Policy