Skip to content

CriPstian/java-to-kotlin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java to Kotlin

Sample project to start off with my presentation.

check Points to cover

Default endpoints

GET /pets

$ http http://localhost:8080/pets

Return a List of all existing hardcoded pets.

Example result:

[
    {
        "age": 3, 
        "breed": "Doggo", 
        "color": "ORANGE", 
        "id": 1, 
        "name": "Capy", 
        "size": "SMALL"
    }, 
    {
        "age": 4, 
        "breed": "Kitty", 
        "color": "BLACK", 
        "hasFur": true, 
        "id": 2, 
        "name": "Tom", 
        "size": "MEDIUM"
    }
]

GET /pets/dogs

$ http http://localhost:8080/pets/dogs

Return a List of all existing hardcoded dogs.

Example result:

[
    {
        "age": 3, 
        "breed": "Doggo", 
        "color": "ORANGE", 
        "id": 1, 
        "name": "Capy", 
        "size": "SMALL"
    }
]

GET /pets/cats

$ http http://localhost:8080/pets/cats

Return a List of all existing hardcoded cats.

Example result:

[
    {
        "age": 4, 
        "breed": "Kitty", 
        "color": "BLACK", 
        "hasFur": true, 
        "id": 2, 
        "name": "Tom", 
        "size": "MEDIUM"
    }
]

POST /pets/dog

$ http POST http://localhost:8080/pets/dog id:=100 name="Dogger" age:=14 breed="Dogger" color="WHITE" size="SMALL"

Post a new dog in the pet array


POST /pets/cat

$ http POST http://localhost:8080/pets/cat id:=100 name="Tommy" age:=5 breed="Kitty" color="BLACK" size="SMALL" 
hasFur:=true

Post a new cat in the pet array

Install kotlin and compile example

How to install
$ # install sdkman
$ curl -s https://get.sdkman.io | bash
$ # install kotlin
$ sdk install kotlin    
Run in console
$ # Start in console
$ kotlinc
Welcome to Kotlin version 1.2.10 (JRE 1.8.0_45-b14)
Type :help for help, :quit for quit
>>> // Run simple command
>>> println("Hello World")
>>> // Exit
>>> :quit
Run a script
$ # Write the simplest script prossible
$ echo 'println("Hello World")' >> SimpleScript.kts
$ # Run example
$ kotlinc -script SimpleScript.kts
Run as a java JAR
$ # Write the simplest program possible
$ echo 'fun main(args: Array<String>) = println("Hello World")' >> HelloWorld.kt
$ # generate jar
$ kotlinc HelloWorld.kt -include-runtime -d hello-world.jar
$ # run program
$ java -jar hello-world.jar

Steps

  1. Clone this repository from GitHub and run mvn spring-boot:run in the console to show nice banner

  2. Present current project and show curl examples

  3. Add kotlin support using IDEA

    Project Structure -> Modules -> Add Kotlin

    Tools -> Kotlin -> Configure Kotlin in Project

    this will add the following in the maven pom file

<project>
    <properties>
        <!-- ... OTHER PROPERTIES ... -->
        <kotlin.version>1.2.10</kotlin.version>
    </properties>
    <dependencies>
        <!-- ... OTHER DEPENDECIES ... -->
        <!-- Target java version 1.8 (other: 1.6, 1.7) -->
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <!-- LATER EDIT: For jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-kotlin</artifactId>
        </dependency>
        <!-- LATER EDIT: Make classes default open for spring to do it's job -->
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-allopen</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <!-- Kotlin test framework -->
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <!-- ... OTHER PLUGINS ... -->
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <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>
                <configuration>
                    <jvmTarget>1.8</jvmTarget>
                    <!-- LATER EDIT: To help spring with kotlin -->
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>testCompile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  1. Create kotlin directory and mark as source root. Build project.

  2. For kotlin and spring integration we must add the spring-kotlin plugin.

  3. For Jackson Kotlin classes support please add com.fasterxml.jackson.module:jackson-module-kotlin to the classpath.

Why?

This project is a start off project to present the power of Kotlin.
We will follow a pattern to be able to give an example for some of Kotlin's new features.

About

Sample project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published