DEV Community

Johnny Boy
Johnny Boy

Posted on • Updated on

How to create your own 'Hello World' REST Service with Spring Boot, Gradle & Kotlin?

Some questions you might have:

What is Spring Boot?

Spring Boot is one of the most popular Java based frameworks. It is open source. It is developed by Pivotal Team. And it is pretty damn popular in the micro services world. See it for yourself.

What is Kotlin?

Kotlin is an Open Source statically typed programming language that targets the JVM, Android, JavaScript and Native. See the reference.

What is a REST Service?

REST stands for Representational State Transfer. And you can find more info about it here.

What is Gradle?

Gradle is an Open Source build automation tool. Similar to Maven, but cooler. See the gradle homepage

Ok, now we can jump into the good stuff...

Get the bootstrap application from Spring Initializr

The guys making Spring Boot are pretty cool, and created a page where you can go and get your new spring boot application already configured and with the dependencies you want.
For this example we are going to use just two:

  • Spring Web
    • Uses Apache Tomcat.
    • Used for building RESTful services.
  • Spring Boot DevTools
    • Allows you to do live reloading and fast application restarts.
  1. You need to go to the Spring Initializr page
  2. Select Project: Gradle Project
  3. Select Language: Kotlin
  4. Leave the Spring Boot and Project Metadata as is.
  5. When you arrive to the Dependencies section search for 'Spring Web' and 'Spring Boot DevTools'
  6. Click Generate(the green button)

Configuration page

After you generate your spring boot application and unzip it.
Your project should look something like this: Folder Structure{width=50px}

Create a new package.

You can name the new package:

com.example.demo.api

Create a new Kotlin Class

Inside this new package, you need to create a new Kotlin class. Which we are going to name:

HelloWorldController

Example: Create Kotlin Class{width=250px}

Map your class to the requests

We need to add two annotations, one to let the framework know that this class is a REST controller and the another one to tell Spring where to send the request with a particular path. Example:

    @RestController 
    @RequestMapping("api/v1/hello")

Create a new function

Now that the request mapping has been added to the class, we need to tell spring boot where are we going to handle the 'world' part of our hello world example.
For that we need to create a new function

    fun getHelloWorld(): String {
        return "Hello world!!!"
    } 

Map the newly created function

Now that we have created a function that will handle the request we need to map it!
To do that, we need to add a new annotation to the method:

    @GetMapping("/world")

Run the application!

Now that we have everything ready, you can run the application!!!
In order to do this, we have many options. Two of them are:

  1. In the command line run:

    $ gradle bootRun

  2. Or in your IDE hit the 'run' button.

See your Service

To check your new shinny Kotlin RESTful 'Hello World' Service, you can go to http://localhost:8080/api/v1/hello/world

Your Controller should look like this:

package com.example.demo.api

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RequestMapping("api/v1/hello")
@RestController
class HelloWorldController {

    @GetMapping("/world")
    fun getHelloWorld(): String {
        return "Hello world!!!"
    }

}

The rest of the folder structure and files, should remain as they were when you downloaded the bootstrap project.

Stay tuned for more

Some upcoming articles will be: Dockerize it, using JPA, configure Swagger, failure tolerance, service discovery.

The END

Top comments (0)