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
Once installed, create a folder and run khipster
to scaffold the application.
mkdir app-kotlin
cd app-kotlin
khipster
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.
ā 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 { ... }
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() { ... }
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
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
Explore yourself: Create a couple of entities and check the generated files. - Refer