Kotlin - Spring

  • Introduction

    The Spring Framework provides first-class support for Kotlin and lets developers write Kotlin applications almost as if the Spring Framework was a native Kotlin framework. Most of the code samples of the reference documentation are provided in Kotlin in addition to Java.

    Spring Framework - Kotlin

    Spring Boot

    Spring Boot is a framework that simplifies the creation of stand-alone, Spring based applications.

    Create a new “JVM Application” with Amper

    To enable Spring Boot support, add the following to the module.yaml file:

    settings:
    springBoot: enabled

    Mixed projects (containing Java and Kotlin sources simultaneously) are supported.

    Examples of Spring Boot projects:

    Amper - Spring

    RESTful Web Service

    You will build a service that will accept HTTP GET requests at http://localhost:8080/greeting.

    It will respond with a JSON representation of a greeting, as the following listing shows:

    {"id":1,"content":"Hello, World!"}

    You can customize the greeting with an optional name parameter in the query string, as the following listing shows:

    http://localhost:8080/greeting?name=User

    The name parameter value overrides the default value of World and is reflected in the response, as the following listing shows:

    {"id":1,"content":"Hello, User!"}

    Create a Resource Representation Class

    The service will handle GET requests for /greeting, optionally with a name parameter in the query string. The GET request should return a 200 OK response with JSON in the body that represents a greeting. It should resemble the following output:

    {
    "id": 1,
    "content": "Hello, World!"
    }

    The id field is a unique identifier for the greeting, and content is the textual representation of the greeting.

    To model the greeting representation, create a resource representation class. To do so, provide a data class for the id and content data, as the following listing shows:

    data class Greeting(val id: Long, val content: String)
    Note

    This application uses the Jackson JSON library to automatically marshal instances of type Greeting into JSON. Jackson is included by default by the web starter.

    Create a Resource Controller

    In Spring’s approach to building RESTful web services, HTTP requests are handled by a controller. These components are identified by the @RestController annotation, and the GreetingController class shown in the following listing handles GET requests for /greeting by returning a new instance of the Greeting class:

    private const val template = "Hello, %s!"
    @RestController
    class GreetingController {
    private val counter = AtomicLong(0)
    @GetMapping("/greeting")
    fun greeting(@RequestParam name: String = "World") =
    Greeting(counter.fetchAndAdd(1), template.format(name))
    }

    Building a RESTful Web Service

    Test the Service

    http://localhost:8080/greeting?name=david

    Test

    Spring Boot provides a @SpringBootTest annotation that works by creating the ApplicationContext used in your tests through SpringApplication. In addition to @SpringBootTest a number of other annotations are also provided for testing more specific slices of an application.

    By default, @SpringBootTest will not start a server. You can use the webEnvironment attribute of @SpringBootTest to further refine how your tests run:

    Pending