Spring Boot + Kotlin + AngularJS + WebJar CRUD Example

In previous part of Spring Boot + Kotlin + AngularJS + WebJar CRUD Example we see the basic tutorial in which create Spring Boot application and in this part we’ll add Kotlin dependency in Spring Boot application.

What is and Why Kotlin?

Kotlin is a general purpose, statically type and open-source programming language. Kotlin targets Java, Android, JavaScript, etc. It is run on JVM. It can be used where Java is used today, such as Android apps development, server side development and much more.

See this : Kotlin Problems and How to avoid them?

Features of Kotlin

  • Concise: Kotlin reduces writing the extra codes. This make Kotlin more concise.
  • Null safety: Kotlin is null safety language. Kotlin aimed to eliminate the NullPointerException (null reference) from the code.Interoperable:
  • Interoperable: Kotlin easily call the Java code in a natural way as well as Kotlin code can be used by Java.
  • Smart cast: It explicitly typecast the immutable values and inserts the value in its safe cast automatically.
  • Compilation Time: It has better performance and fast compilation time.
  • Tool-friendly: Kotlin program are build using command line as well as any of Java IDE.
  • Extension function: Kotlin supports extension functions and extension properties. Which means it helps to extend the functionality of classes without touching their code.

File Structure

You’ll have to delete SpringBootCrudApplication.java from co.tellmehow.kotlin package and add Kotlin class(SpringBootCrudApplication.kt) instead of it. And now file structure would be:

SpringBootCrudApplication.kt

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.ComponentScan

@EnableAutoConfiguration
@ComponentScan
class SpringBootCrudApplication

fun main(args: Array<String>)
{
  SpringApplication.run(SpringBootCrudApplication::class.java, *args)
  println(" **** Spring Boot + Kotlin + AngularJS CRUD Example!!! *****")
}

Kotlin dependency in pom.xml

Add Kotlin version inside properties tag(<properties>)

    <java.version>1.8</java.version>
    <kotlin.version>1.1.4-3</kotlin.version>

Add Kotlin dependencies

<!-- Kotlin -->
    
    <dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-stdlib-jre8</artifactId>
      <version>${kotlin.version}</version>
    </dependency>
    <dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-reflect</artifactId>
      <version>${kotlin.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.module</groupId>
      <artifactId>jackson-module-kotlin</artifactId>
      <version>2.9.0</version>
    </dependency>

Now add Kotlin plugins inside plugin tag(<plugins>)

<!-- Kotlin plugins -->
      
      <plugin>
        <artifactId>kotlin-maven-plugin</artifactId>
        <groupId>org.jetbrains.kotlin</groupId>
        <version>${kotlin.version}</version>
        <configuration>
          <compilerPlugins>
            <plugin>spring</plugin>
          </compilerPlugins>
          <jvmTarget>1.8</jvmTarget>
        </configuration>
        <executions>
          <execution>
            <id>compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
          <execution>
            <id>test-compile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>test-compile</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-allopen</artifactId>
            <version>${kotlin.version}</version>
          </dependency>
        </dependencies>
      </plugin>

Note: Don’t worry about pom.xml. I’ll give you full pom.xml code in last part.

Run your SpringBootKotlinApplication.kt and your SpringBoot application is now connect with Kotlin.

In next part we’ll create REST service using Spring Boot with Kotlin which will use as backend of our CRUD application. So stay with us.

By Tell Me How

It is a technology blog and admin has excellent experience in programming from 5+ year. You can contact us at ceo.tellmehow@gmail.com

Share your thoughts

Leave a Reply

Loading Facebook Comments ...
Loading Disqus Comments ...