Less boilerplate: Crnk with Kotlin and Spring Boot
Tags: kotlin, rest, crnk, dev
Published: 2020-06-21 15:42:10

---
title: Less boilerplate: Crnk with Kotlin and Spring Boot
published: true
description: Template for Spring Boot project using Kotlin and Crnk
tags: #kotlin #rest #crnk #dev
cover_image: https://images.unsplash.com/photo-1472035846169-06348f2377fa?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1950&q=80
---
I grew tired of writing the same code over and over again. 

When writing RESTful APIs, you tend to add GET and POST endpoints to your project and as it grows, so do the lines of repetitive code. Not only do your controllers explode - using plain Java leads to writing Getters and Setters in your object classes.

For the latter, best practice is to use [Lombok](https://projectlombok.org/), a library that offers you a variety of Annotations to get rid of the boilerplate code for getting and setting properties. But what about those Controllers? 

## Introducing Crnk

Somebody told me about [crnk.io](https://github.com/crnk-project/crnk-framework). Crnk is a library where you only have to define your resources and relations between them, implement the repositories - everything else is taken care of by Crnk: the Controllers, validation, error handling, HATEOAS. It even follows the [JSON:API specification](https://jsonapi.org/) for building APIs. So, the format of your responses is consistent throughout the whole application! Bonus points: You even get a nice UI to access your resources.

![alt text](https://raw.githubusercontent.com/Schmowser/crnk-kotlin/master/crnk-ui.png)

I was mesmerized but, personally, I missed a working example with Kotlin. So, I created one myself. If you want to start right away or need such a template, feel free to fork [my repository on Github](https://github.com/Schmowser/crnk-kotlin).

## Setting up Crnk with Kotlin and Spring Boot from scratch

This is what you have to do if you want to set up Crnk with a Spring Boot project from scratch. With [Spring Initializr](https://start.spring.io/) create a Spring Boot Kotlin project and execute the following steps: 

* Replace mavenCentral with jcenter in the gradle build file
```
repositories {
	jcenter()
}
```

* Import crnk maven bom
```
configure<io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension> {
	imports {
		mavenBom("io.crnk:crnk-bom:3.2.20200419165537")
	}
}
```
The versions can be found [here](https://bintray.com/crnk-project/maven/crnk-bom).

* Import crnk dependencies
```
dependencies {
	implementation("io.crnk:crnk-home")
	implementation("io.crnk:crnk-setup-spring-boot2")
	implementation("io.crnk:crnk-ui")
}
```
* Write the code!

Looking at the result, you will notice that you only have to write the model (data class) and to implement the repository (extends a `ResourceRepositoryBase`). It is important that you need to define a `NoArgsConstructor` in the data class in order to make it work for Kotlin. But then, all is set and you can say goodbye to keeping in mind all the intricacies of setting up new controllers each time you introduce new resources.
___

_More details and the source code for the template for a Spring Boot Kotlin project with Crnk can be found [here](https://github.com/Schmowser/crnk-kotlin)._