My journey into SpringBoot with Kotlin
I have been an adept C# .Net-ter and always will be. One of my favourites is still C# .NET Core as I do C# almost on a daily basis, but a side of me wanted to be platform and framework agnostics as much as I can. I have been taking advantage of tradeoffs in the different languages/framework in developing micro-services. Some argue that you've got to master one language really well and then one framework really well. I say, You can master them all, be proficient and be able to develop production ready applications. In my case, I currently do .Net Core (MVC/Web API), Spring Boot (Java, Kotlin (preferred)), Python (Django, Flask(preferred)), JavaScript/TypeScript is also worth mentioning, but that's another topic for another day. In a future post, I shall detail a small project using all these technology and framework in a architecture of micro-services . As I sink my feet more into the world of micro-services, and the plethora of technologies that can come together and function as a big ol system, it is a fascinating and yet intriguing experience. Having had a strong Java programming experience under my belt, I was able to get started quickly with Kotlin and the transition is almost none existent. I'd say flawless. I have been using Kotlin in production at work and it's a pure joy to work with in comparison to Java. An analogy to explain Kotlin vs Java would be taking the elevator or using the stairs to get to the 100th floor of a building, and Kotlin is the elevator. One catch is that Kotlin compiles to Java in the background. More also, Kotlin and Java can interop really well. You can extend your existing Java codes with Kotlin or mix-match them.
With the knowledge of Java, I do catch myself inspecting the compiled Java code to see the nitty-gritty of what's happening under the hood when I come across some not so understandable Kotlin syntax. No matter how Kotlin syntax looks on IntelliJ Idea, my god, it's still beautiful wow!
Let's say we want to create an InvoiceModel class that represents an invoice for a nursery. The properties of the class should allow for change or update. Let's see this in Java and Kotlin.
//InvoiceModel.java
public class InvoiceModel {
private String fullName;
private String invoicedate;
private String term;
private List<InvoiceData> invoiceDatas;
private Integer breakfasttotal;
private Integer lunchtotal;
private Integer afterSchooltotal;
private double breakfastamount;
private double lunchamount;
private double afterschoolamount;
private double totaldue;
private double breakfastprice;
private double lunchprice;
private double afterschoolprice;
public InvoiceModel(String fullName, String invoicedate, String term, List<InvoiceData> invoiceDatas,
Integer breakfasttotal, Integer lunchtotal, Integer afterSchooltotal, double breakfastamount,
double lunchamount, double afterschoolamount, double totaldue, double breakfastprice, double lunchprice,
double afterschoolprice) {
super();
this.fullName = fullName;
this.invoicedate = invoicedate;
this.term = term;
this.invoiceDatas = invoiceDatas;
this.breakfasttotal = breakfasttotal;
this.lunchtotal = lunchtotal;
this.afterSchooltotal = afterSchooltotal;
this.breakfastamount = breakfastamount;
this.lunchamount = lunchamount;
this.afterschoolamount = afterschoolamount;
this.totaldue = totaldue;
this.breakfastprice = breakfastprice;
this.lunchprice = lunchprice;
this.afterschoolprice = afterschoolprice;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getInvoicedate() {
return invoicedate;
}
public void setInvoicedate(String invoicedate) {
this.invoicedate = invoicedate;
}
public String getTerm() {
return term;
}
public void setTerm(String term) {
this.term = term;
}
public List<InvoiceData> getInvoiceDatas() {
return invoiceDatas;
}
public void setInvoiceDatas(List<InvoiceData> invoiceDatas) {
this.invoiceDatas = invoiceDatas;
}
public Integer getBreakfasttotal() {
return breakfasttotal;
}
public void setBreakfasttotal(Integer breakfasttotal) {
this.breakfasttotal = breakfasttotal;
}
public Integer getLunchtotal() {
return lunchtotal;
}
public void setLunchtotal(Integer lunchtotal) {
this.lunchtotal = lunchtotal;
}
public Integer getAfterSchooltotal() {
return afterSchooltotal;
}
public void setAfterSchooltotal(Integer afterSchooltotal) {
this.afterSchooltotal = afterSchooltotal;
}
public double getBreakfastamount() {
return breakfastamount;
}
public void setBreakfastamount(double breakfastamount) {
this.breakfastamount = breakfastamount;
}
public double getLunchamount() {
return lunchamount;
}
public void setLunchamount(double lunchamount) {
this.lunchamount = lunchamount;
}
public double getAfterschoolamount() {
return afterschoolamount;
}
public void setAfterschoolamount(double afterschoolamount) {
this.afterschoolamount = afterschoolamount;
}
public double getTotaldue() {
return totaldue;
}
public void setTotaldue(double totaldue) {
this.totaldue = totaldue;
}
public double getBreakfastprice() {
return breakfastprice;
}
public void setBreakfastprice(double breakfastprice) {
this.breakfastprice = breakfastprice;
}
public double getLunchprice() {
return lunchprice;
}
public void setLunchprice(double lunchprice) {
this.lunchprice = lunchprice;
}
public double getAfterschoolprice() {
return afterschoolprice;
}
public void setAfterschoolprice(double afterschoolprice) {
this.afterschoolprice = afterschoolprice;
}
}
Now Kotlin
// InvoiceModel.kt
data class InvoiceModel(
var fullName: String,
var invoicedate: String,
var term: String,
var invoiceDatas: List<InvoiceData>,
var breakfasttotal: Int,
var lunchtotal: Int,
var afterSchooltotal: Int,
var breakfastamount: Double,
var lunchamount: Double,
var afterschoolamount: Double,
var totaldue: Double,
var breakfastprice: Double,
var lunchprice: Double,
var afterschoolprice: Double
)
Just look at the beauty. Now compare the Java's 151 lines of code to Kotlin's 17 lines. Wow! Simply beautiful isn't it?
Convention over Configuration
Spring Boot abstracts all of the configuration you would have to do with the Spring framework. But if you would like to control or extend the default configurations, Spring Boot provides application.properties for this purpose.
Dependency Injection
Right out of the box, you get dependency injection for free with Spring Boot. The framework does this uplifting for you and you just worry about writing codes rather than registering your types explicitly like in .NET Core. Oh boy they got this right with Spring Boot.
Modular Boilerplate Code
You can generate your Spring Boot boiler code at http://start.spring.io to build the tempplate for your project and you can make it as modular as you want. They include but not limited to:
- Spring Data
- Spring Security
- Spring MVC and much more...
Conclusion
Getting started with the Spring framework using Spring Boot is very interesting if you already know Java as the framework is Java based, but came along Kotlin which makes it super fun to work with. You should easily get started with Kotlin if you are already comfortable with Java. You can learn Kotlin from scratch, but you will be better off knowing some Java at least the basics as most of the library and import you will be doing in Kotlin, are Java libraries. If you want to get started with developing Java based enterprise application, the Spring framework via Spring Boot is matured to take you there and Kotlin over Java should be your choice. Future post will talk about how to get started with Spring Boot using Kotlin. Happy Coding!