Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified docs/images/spring-boot/create-database-schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/spring-boot/create-spring-boot-project.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/spring-boot/execute-get-requests.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/spring-boot/execute-post-requests.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/spring-boot/retrieve-message-by-its-id.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/spring-boot/run-spring-boot-application.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/spring-boot/set-up-spring-boot-project.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/spring-boot/spring-boot-project-view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 20 additions & 17 deletions docs/topics/jvm/jvm-create-project-with-spring-boot.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Download and install the latest version of [IntelliJ IDEA Ultimate Edition](http
Create a new Spring Boot project with Kotlin by using the Project Wizard in IntelliJ IDEA Ultimate Edition:

1. In IntelliJ IDEA, select **File** | **New** | **Project**.
2. In the panel on the left, select **New Project** | **Spring Boot**.
2. In the panel on the left, select **Spring Boot** in the **Generators** section.
3. Specify the following fields and options in the **New Project** window:

* **Name**: demo
Expand All @@ -52,7 +52,7 @@ Create a new Spring Boot project with Kotlin by using the Project Wizard in Inte
>
{style="tip"}

![Create Spring Boot project](create-spring-boot-project.png){width=800}
![Create Spring Boot project](create-spring-boot-project.png){width=700}

4. Ensure that you have specified all the fields and click **Next**.

Expand All @@ -62,7 +62,7 @@ Create a new Spring Boot project with Kotlin by using the Project Wizard in Inte
* **SQL | Spring Data JDBC**
* **SQL | H2 Database**

![Set up Spring Boot project](set-up-spring-boot-project.png){width=800}
![Set up Spring Boot project](set-up-spring-boot-project.png){width=700}

6. Click **Create** to generate and set up the project.

Expand Down Expand Up @@ -109,19 +109,21 @@ repositories {
}

dependencies {
implementation("org.springframework.boot:spring-boot-h2console")
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin") // Jackson extensions for Kotlin for working with JSON
implementation("org.springframework.boot:spring-boot-starter-webmvc")
implementation("org.jetbrains.kotlin:kotlin-reflect") // Kotlin reflection library, required for working with Spring
implementation("tools.jackson.module:jackson-module-kotlin") // Jackson extensions for Kotlin for working with JSON
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.boot:spring-boot-starter-data-jdbc-test")
testImplementation("org.springframework.boot:spring-boot-starter-webmvc-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

kotlin {
compilerOptions {
freeCompilerArgs.addAll("-Xjsr305=strict") // `-Xjsr305=strict` enables the strict mode for JSR-305 annotations
freeCompilerArgs.addAll("-Xjsr305=strict", "-Xannotation-default-target=param-property") // `-Xjsr305=strict` enables the strict mode for JSR-305 annotations
}
}

Expand All @@ -134,13 +136,15 @@ As you can see, there are a few Kotlin-related artifacts added to the Gradle bui

1. In the `plugins` block, there are two Kotlin artifacts:

* `kotlin("jvm")` – the plugin defines the version of Kotlin to be used in the project
* `kotlin("plugin.spring")` – Kotlin Spring compiler plugin for adding the `open` modifier to Kotlin classes in order to make them compatible with Spring Framework features
* The `kotlin("jvm")` plugin defines the version of Kotlin to be used in the project.
* The Kotlin Spring compiler plugin, `kotlin("plugin.spring")`, adds the `open` modifier to Kotlin classes to make
them compatible with Spring Framework features

2. In the `dependencies` block, a few Kotlin-related modules listed:
2. In the `dependencies` block, a few Kotlin-related modules are listed:

* `com.fasterxml.jackson.module:jackson-module-kotlin` – the module adds support for serialization and deserialization of Kotlin classes and data classes
* `org.jetbrains.kotlin:kotlin-reflect` – Kotlin reflection library
* The `tools.jackson.module:jackson-module-kotlin` module adds support for serialization and deserialization of Kotlin
classes and data classes.
* `org.jetbrains.kotlin:kotlin-reflect` is a Kotlin reflection library.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an idea: "is the Kotlin reflection library to ..."


3. After the dependencies section, you can see the `kotlin` plugin configuration block.
This is where you can add extra arguments to the compiler to enable or disable various language features.
Expand Down Expand Up @@ -172,8 +176,8 @@ fun main(args: Array<String>) {
<p>In Kotlin, if a class doesn't include any members (properties or functions), you can omit the class body (<code>{}</code>) for good.</p>
</def>
<def title="@SpringBootApplication annotation">
<p><a href="https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.using-the-springbootapplication-annotation"><code>@SpringBootApplication annotation</code></a> is a convenience annotation in a Spring Boot application.
It enables Spring Boot's <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.auto-configuration">auto-configuration</a>, <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html">component scan</a>, and be able to define an extra configuration on their "application class".
<p><a href="https://docs.spring.io/spring-boot/reference/using/using-the-springbootapplication-annotation.html#using.using-the-springbootapplication-annotation"><code>@SpringBootApplication annotation</code></a> is a convenience annotation in a Spring Boot application.
It enables Spring Boot's <a href="https://docs.spring.io/spring-boot/reference/using/auto-configuration.html#using.auto-configuration">auto-configuration</a>, <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html">component scan</a>, and be able to define an extra configuration on their "application class".
</p>
</def>
<def title="Program entry point – main()">
Expand All @@ -192,7 +196,6 @@ fun main(args: Array<String>) {
</def>
</deflist>


## Create a controller

The application is ready to run, but let's update its logic first.
Expand Down Expand Up @@ -253,7 +256,7 @@ The Spring application is now ready to run:

1. In the `DemoApplication.kt` file, click the green **Run** icon in the gutter beside the `main()` method:

![Run Spring Boot application](run-spring-boot-application.png){width=706}
![Run Spring Boot application](run-spring-boot-application.png){width=700}

> You can also run the `./gradlew bootRun` command in the terminal.
>
Expand All @@ -269,7 +272,7 @@ The Spring application is now ready to run:

You should see "Hello, John!" printed as a response:

![Spring Application response](spring-application-response.png){width=706}
![Spring Application response](spring-application-response.png){width=700}

## Next step

Expand Down
12 changes: 6 additions & 6 deletions docs/topics/jvm/jvm-spring-boot-add-data-class.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ It requires changing the `MessageController` class to respond with a JSON docume
</list>
<p>The corresponding factory functions are also provided by the Kotlin Standard Library to create instances of such collections.
</p>
<p>In this tutorial, you use the <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/list-of.html"><code>listOf()</code></a> function to create a list of <code>Message</code> objects.
<p>In this tutorial, you use the <a href="https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/list-of.html"><code>listOf()</code></a> function to create a list of <code>Message</code> objects.
This is the factory function to create a <i>read-only</i> list of objects: you can't add or remove elements from the list.<br/>
If it is required to perform write operations on the list, call the <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-list-of.html"><code>mutableListOf()</code></a> function to create a mutable list instance.
If it is required to perform write operations on the list, call the <a href="https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/mutable-list-of.html"><code>mutableListOf()</code></a> function to create a mutable list instance.
</p>
</def>
<def title="Trailing comma">
Expand All @@ -102,8 +102,8 @@ It requires changing the `MessageController` class to respond with a JSON docume

The response from `MessageController` will now be a JSON document containing a collection of `Message` objects.

> Any controller in the Spring application renders JSON response by default if Jackson library is on the classpath.
> As you [specified the `spring-boot-starter-web` dependency in the `build.gradle.kts` file](jvm-create-project-with-spring-boot.md#explore-the-project-gradle-build-file), you received Jackson as a _transitive_ dependency.
> 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.md#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.
>
{style="note"}
Expand Down Expand Up @@ -169,10 +169,10 @@ The Spring application is ready to run:

You will see a page with a collection of messages in JSON format:

![Run the application](messages-in-json-format.png){width=800}
![Run the application](messages-in-json-format.png){width=700}

## Next step

In the next part of the tutorial, you'll add and configure a database to your project, and make HTTP requests.
In the next part of the tutorial, you'll add and configure a database to your project and make HTTP requests.

**[Proceed to the next chapter](jvm-spring-boot-add-db-support.md)**
24 changes: 11 additions & 13 deletions docs/topics/jvm/jvm-spring-boot-add-db-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ For convenience, the Spring Framework provides the `JdbcTemplate` class that sim

## Add database support

The common practice in Spring Framework based applications is to implement the database access logic within the so-called _service_ layer – this is where business logic lives.
The common practice in Spring Framework-based applications is to implement the database access logic within the so-called _service_ layer – this is where business logic lives.
In Spring, you should mark classes with the `@Service` annotation to imply that the class belongs to the service layer of the application.
In this application, you will create the `MessageService` class for this purpose.

Expand All @@ -25,7 +25,6 @@ package com.example.demo

import org.springframework.stereotype.Service
import org.springframework.jdbc.core.JdbcTemplate
import java.util.*

@Service
class MessageService(private val db: JdbcTemplate) {
Expand Down Expand Up @@ -69,7 +68,7 @@ class MessageService(private val db: JdbcTemplate) {
</def>
<def title="Underscore for unused lambda argument">
<p>For a lambda with multiple parameters, you can use the underscore <code>_</code> character to replace the names of the parameters you don't use.</p>
<p>Hence, the final syntax for query function call looks like this:</p>
<p>Hence, the final syntax for the query function call looks like this:</p>
<code-block lang="kotlin">
db.query("select * from messages") { response, _ ->
Message(response.getString("id"), response.getString("text"))
Expand Down Expand Up @@ -110,7 +109,7 @@ class MessageController(private val service: MessageService) {

<deflist collapsible="true">
<def title="@PostMapping annotation">
<p>The method responsible for handling HTTP POST requests needs to be annotated with <code>@PostMapping</code> annotation. To be able to convert the JSON sent as HTTP Body content into an object, you need to use the <code>@RequestBody</code> annotation for the method argument. Thanks to having Jackson library in the classpath of the application, the conversion happens automatically.</p>
<p>The method responsible for handling HTTP POST requests needs to be annotated with <code>@PostMapping</code> annotation. To be able to convert the JSON sent as HTTP Body content into an object, you need to use the <code>@RequestBody</code> annotation for the method argument. Thanks to having the Jackson library in the classpath of the application, the conversion happens automatically.</p>
</def>
<def title="ResponseEntity">
<p><code>ResponseEntity</code> represents the whole HTTP response: status code, headers, and body.</p>
Expand All @@ -137,7 +136,6 @@ package com.example.demo

import org.springframework.stereotype.Service
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.query
import java.util.UUID

@Service
Expand Down Expand Up @@ -169,9 +167,9 @@ The application code is ready to work with the database. It is now required to c

Configure the database in the application:

1. Create `schema.sql` file in the `src/main/resources` directory. It will store the database object definitions:
1. Create the `schema.sql` file in the `src/main/resources` directory. It will store the database object definitions:

![Create database schema](create-database-schema.png){width=400}
![Create database schema](create-database-schema.png){width=350}

2. Update the `src/main/resources/schema.sql` file with the following code:

Expand Down Expand Up @@ -242,11 +240,11 @@ You should use an HTTP client to work with previously created endpoints. In Inte
3. Execute all POST requests. Use the green **Run** icon in the gutter next to the request declaration.
These requests write the text messages to the database:

![Execute POST request](execute-post-requests.png)
![Execute POST request](execute-post-requests.png){width=700}

4. Execute the GET request and see the result in the **Run** tool window:

![Execute GET requests](execute-get-requests.png)
![Execute GET requests](execute-get-requests.png){width=700}

### Alternative way to execute requests {initial-collapse-state="collapsed" collapsible="true"}

Expand Down Expand Up @@ -314,7 +312,7 @@ Extend the functionality of the application to retrieve the individual messages
</def>
</deflist>

> The `.query()` function that is used to fetch the message by its id is a [Kotlin extension function](extensions.md#extension-functions)
> The `.query()` function used to fetch the message by its id is a [Kotlin extension function](extensions.md#extension-functions)
> provided by the Spring Framework. It requires an additional import `import org.springframework.jdbc.core.query` as demonstrated in the code above.
>
{style="warning"}
Expand Down Expand Up @@ -479,7 +477,7 @@ The Spring application is ready to run:

3. Execute the GET request to retrieve all the messages from the database.

4. In the **Run** tool window copy one of the ids and add it to the request, like this:
4. In the **Run** tool window, copy one of the ids and add it to the request, like this:

```http request
### Get the message by its id
Expand All @@ -492,10 +490,10 @@ The Spring application is ready to run:

5. Execute the GET request and see the result in the **Run** tool window:

![Retrieve message by its id](retrieve-message-by-its-id.png){width=706}
![Retrieve message by its id](retrieve-message-by-its-id.png){width=700}

## Next step

The final step shows you how to use more popular connection to database using Spring Data.
The final step shows you how to use a more popular connection to the database using Spring Data.

**[Proceed to the next chapter](jvm-spring-boot-using-crudrepository.md)**
Loading