DEV Community

Cover image for Kotlin Hipster = Spring Boot + Kotlin
Sendil Kumar
Sendil Kumar

Posted on • Updated on • Originally published at sendilkumarn.com

Kotlin Hipster = Spring Boot + Kotlin

Scripts are awesome isn't it? Remember how we generate applications few years before, with scripts.

Bootstrapping an application is a mundane task. With changing technologies everyday, spending a lot of time for these mundane task is not worth it.

During those furious hours of debugging a broken project generation script, I found JHipster and fell in love with it immediately ❀️.

At the core, JHipster generates an application and it has all the bells and whistles.

Over the years, the awesome community made JHipster even more awesome. 😎 πŸ‘ ✨

JHipster grew exponentially. Feature after feature entered into the world of JHipster. This bloated our codebase and made it harder to maintain.

At this point, we introduced the concept called blueprints. Blueprints are small sub-generator that hooks on to different stages of the JHipster's main generator.

For example, In JHipster we have a backend generator (server) and a frontend generator (client). With Blueprint you can modify any or both the generators either completely or partially.

Kotlin

I am biased towards statically typed languages and love a language that allows me to write concise and expressive code. This is what attracted me towards Kotlin at first.

It is for a reason Kotlin is the 4th fastest growing language. The language provides you an option to write elegant, concise and expressive code.

Kotlin is an interesting language. Kotlin has interesting parts from various different language and included that into the language.

As Venkat used to say, it removes all the ceremonies from the code.

With Google announcing Kotlin everywhere and Spring team adopting Kotlin, I wanted to experience Kotlin.

This is where I started experimenting with the Kotlin Hipster.

Kotlin Hipster a.k.a KHipster

KHipster has all the good parts of Kotlin and JHipster. It now generates 100% Kotlin based backend along with any option that you choose in JHipster.

Now let us start creating a Kotlin Hipster application.

First install the generator-jhipster-kotlin package.

npm i -g generator-jhipster-kotlin
Enter fullscreen mode Exit fullscreen mode

Once installed, create a folder and run khipster to scaffold the application.

mkdir app-kotlin
cd app-kotlin
khipster
Enter fullscreen mode Exit fullscreen mode

This will ask you a set of questions:

tl:dr; If you have used JHipster these questions are the same.

We will generate a monolithic application with SQL database. We will add stateless JWT based authentication and use Gradle as the build tool. We will use React for the frontend.

Answers for KHipster command

βœ… That is it, you have generated the spring boot application with Kotlin in the backend and React frontend.

This is a sample entity class. We define the entity class with a data keyword. This also removes the need of defining any getters and setters. It completely removes all the boilerplate code that you will need in the application.

@Entity
@Table(name = "jhi_authority")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
data class Authority(

    @field:NotNull
    @field:Size(max = 50)
    @Id
    @Column(length = 50)
    var name: String? = null

) : Serializable { ... }
Enter fullscreen mode Exit fullscreen mode

This is a SecurityCondfiguration bean. The way we extend a class is also different. Note that there is no extends keyword and only a : followed by the name of the class that is extended with a parenthesis.

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@Import(SecurityProblemSupport::class)
class SecurityConfiguration(
    private val tokenProvider: TokenProvider,
    private val corsFilter: CorsFilter,
    private val problemSupport: SecurityProblemSupport
) : WebSecurityConfigurerAdapter() { ... }

Enter fullscreen mode Exit fullscreen mode

Kotlin is a null-safety language that makes us to think in handling null. Also with native coroutine support it is much easier to write reactive applications. You can even generate a reactive application with KHipster.

There are many awesome features in Kotlin, We will explore Kotlin in the future posts.

Let us run the application using gradle

./gradlew
Enter fullscreen mode Exit fullscreen mode

Now head over to http://localhost:8080 to experience the awesome new khipster application.

The generated application is production ready from day one. Generating entities is similar to JHipster.

khipster entity foo
Enter fullscreen mode Exit fullscreen mode

Explore yourself: Create a couple of entities and check the generated files. - Refer

You can follow me on Twitter.

If you like this article, please leave a like or a comment. ❀️

If you like KHipster, share it within your community and don't forget to star the repo.

Top comments (4)

Collapse
 
rohansawant profile image
Rohan Sawant

This is gold! πŸ₯‡ So, awesome to see more Kotil stuff on here!

Collapse
 
mielech profile image
Michal Mieleszko • Edited
Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard • Edited

Is the React frontend written with Typescript or with Kotlin/JS?
It would be really nice to have both options.

Collapse
 
sendilkumarn profile image
Sendil Kumar

I have mixed feelings about the Kotlin/JS. It is very hard to maintain, if you want to contribute and maintain, I am more than happy with it.