Recently I published a tutorial about the upcoming release of the popular Spring Frameworks. You can read the post here. I am very excited about the next release of Spring and the next release of Java. Past few days, I have been experimenting with the Kotlin language. I think it is a very well designed language and incredibly easy to adopt. First, we heard Google chose Kotlin as a first class citizen to the Android platform. Now, we heard that Spring team decided to treat Kotlin as a first class citizen in Spring Framework. I believe all these developments will help Kotlin to gain popularity and we will see it in a better place in TIOBE index.

Previously, I used maven as the package manager, but in this example, I will use gradle as the package manager. Gradle uses a Groovy DSL to configure dependencies. In a very close future, we will be able to use Kotlin in our Gradle configuration files.

Go to https://start.spring.io and select boxes ‘Generate a Gradle Project with Kotlin and Spring Boot 2.0.0 M3. In ‘Project Metadata’ section write proper group and artifact names. In dependencies section select Web, MongoDB. Click on ‘Generate Project’ button then open downloaded the zip file with your favorite IDE.

This tutorial requires MongoDB, and you can install it via HomeBrew on OS X or use a Docker container as you wish. This tutorial can be thought as a Kotlin translated version of the Accessing Data with MongoDB and Building a RESTful Web Service guides in Spring Guides.

Lets create the model:

data class Customer(@Id val id : String?, val firstName : String, val lastName : String)

As you see, this is class oneliner. If you are familiar with Lombok package in Java, you might remember the @Data annotation. data modifier in Kotlin does the same thing, but it is integrated into the language. Wowww goes here.

The repository:

interface Repository : MongoRepository<Customer, String> {
    fun findByFirstName(firstName : String)
    fun findByLastName(lastName : String)
}

The repository class can be translated to Java line by line.

Seeding some data for the demo:

@Component
class Seeder(private val repository: Repository) : CommandLineRunner {
    override fun run(vararg args: String?) {
        repository.deleteAll()
        repository.save(Customer("5985cff7eed9877e3e5b6976", "John", "Snow"))
        repository.save(Customer("5985cff7eed9877e3e5b6977", "Ned", "Stark"))
        repository.save(Customer("5985cff7eed9877e3e5b6978", "Tyrion", "Lannister"))

        println("Found Customers")
        repository.findAll().forEach(::println)
    }
}

Constructor automatically injects the repository we created before. Just like Lombok’s @Data annotation, override appears as a keyword instead of an annotation.

And finally the Controller:

@RestController
@RequestMapping("/customers")
class Controller(val repository: Repository) {

    @GetMapping("")
    fun index(): List<Customer> = repository.findAll()

    @PostMapping("")
    @ResponseStatus(HttpStatus.CREATED)
    fun create(@RequestBody customer: Customer) = repository.save(customer)!!
}

curl http://localhost:8080/customers curl -X POST -H "content-type: application/json" -d '{"firstName": "John", "lastName": "Doe"}' http://localhost:8080/customers

You can try to extend the example yourself by implementing the update and the delete methods. If you have any questions, do not hesitate to shoot below.

References:

  1. https://spring.io/guides/gs/accessing-data-mongodb/
  2. https://spring.io/guides/gs/rest-service/