# Add a data class to Spring Boot project In this part of the tutorial, you'll add some more functionality to the application and discover more Kotlin language features, such as data classes. It requires changing the `MessageController` class to respond with a JSON document containing a collection of serialized objects. ## Update your application 1. In the same package, next to the `DemoApplication.kt` file, create a `Message.kt` file. 2. In the `Message.kt` file, create a data class with two properties: `id` and `text`: ```KOTLIN // Message.kt package com.example.demo data class Message(val id: String?, val text: String) ``` `Message` class will be used for data transfer: a list of serialized `Message` objects will make up the JSON document that the controller is going to respond to the browser request. Data classes – data class Message : The main purpose of [data classes](data-classes.html) in Kotlin is to hold data. Such classes are marked with the `data` keyword, and some standard functionality and some utility functions are often mechanically derivable from the class structure. : In this example, you declared `Message` as a data class as its main purpose is to store the data. val and var properties : [Properties in Kotlin](properties.html) classes can be declared either as: : * mutable, using the `var` keyword * read-only, using the `val` keyword : The `Message` class declares two properties using `val` keyword, the `id` and `text`. The compiler will automatically generate the getters for both of these properties. It will not be possible to reassign the values of these properties after an instance of the `Message` class is created. Nullable types – String? : Kotlin provides [built-in support for nullable types](null-safety.html#nullable-types-and-non-nullable-types). In Kotlin, the type system distinguishes between references that can hold `null` (nullable references) and those that cannot (non-nullable references). For example, a regular variable of type `String` cannot hold `null`. To allow nulls, you can declare a variable as a nullable string by writing `String?`. : The `id` property of the `Message` class is declared as a nullable type this time. Hence, it is possible to create an instance of `Message` class by passing `null` as a value for `id`: : ```KOTLIN Message(null, "Hello!") ``` 3. In the `MessageController.kt` file, instead of the `index()` function, create the `listMessages()` function returning a list of `Message` objects: ```KOTLIN // MessageController.kt package com.example.demo import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController @RestController @RequestMapping("/") class MessageController { @GetMapping fun listMessages() = listOf( Message("1", "Hello!"), Message("2", "Bonjour!"), Message("3", "Privet!"), ) } ``` Collections – listOf() : The Kotlin standard library provides implementations for basic collection types: sets, lists, and maps. Each collection type can be read-only or mutable: : * A read-only collection comes with operations for accessing collection elements. * A mutable collection comes also with write operations for adding, removing, and updating its elements. : The corresponding factory functions are also provided by the Kotlin standard library to create instances of such collections. : In this tutorial, you use the [listOf()](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/list-of.html) function to create a list of `Message` objects. This is the factory function to create a read-only list of objects: you can't add or remove elements from the list. If it is required to perform write operations on the list, call the [mutableListOf()](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/mutable-list-of.html) function to create a mutable list instance. Trailing comma : A [trailing comma](coding-conventions.html#trailing-commas) is a comma symbol after the last item of a series of elements: : ```KOTLIN Message("3", "Privet!"), ``` : This is a convenient feature of Kotlin syntax and is entirely optional – your code will still work without them. : In the example above, creating a list of `Message` objects includes the trailing comma after the last `listOf()` function argument. The response from `MessageController` will now be a JSON document containing a collection of `Message` objects. Note: Any controller in the Spring application renders a JSON response by default if the Jackson library is on the classpath. As you [specified the spring-boot-starter-webmvc dependency in the build.gradle.kts file](jvm-create-project-with-spring-boot.html#explore-the-project-gradle-build-file), you received Jackson as a transitive dependency. Hence, the application responds with a JSON document if the endpoint returns a data structure that can be serialized to JSON. Here is a complete code of the `DemoApplication.kt`, `MessageController.kt`, and `Message.kt` files: ```KOTLIN // DemoApplication.kt package com.example.demo import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication @SpringBootApplication class DemoApplication fun main(args: Array) { runApplication(*args) } ``` ```KOTLIN // MessageController.kt package com.example.demo import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController @RestController @RequestMapping("/") class MessageController { @GetMapping fun listMessages() = listOf( Message("1", "Hello!"), Message("2", "Bonjour!"), Message("3", "Privet!"), ) } ``` ```KOTLIN // Message.kt package com.example.demo data class Message(val id: String?, val text: String) ``` ## Run the application The Spring application is ready to run: 1. Run the application again. 2. Once the application starts, open the following URL: ```TEXT http://localhost:8080 ``` You will see a page with a collection of messages in JSON format: ![Run the application](images/messages-in-json-format.png) ## Next step In the next part of the tutorial, you'll add and configure a database to your project and make HTTP requests. * [Previous step](jvm-create-project-with-spring-boot.html) * [Next step](jvm-spring-boot-add-db-support.html)