In this Spring boot Kotlin step-by-step tutorial, we will learn how to build CRUD ( Create, Read, Update and Delete) Restful APIs with Kotlin, Spring Boot, JPA/Hibernate, and MySQL database.
What will we build?
In this tutorial, we will build REST APIs for creating, retrieving, updating, and deleting a Post.
We’ll use MySQL as our data source and JPA & Hibernate to access the data from the database.
Development Process
We will implement this tutorial step by step:
1. Creating the Spring Boot Application
2. Configure MySQL Database
3. Create JPA Entity - Post.kt
4. Create JPA Repository - PostRepository.kt
5. Create Spring MVC Controller - PostController.kt
6. Running the Application
7. Testing the Rest APIs
Let's begin with creating a spring boot application.
1. Creating the Spring Boot Application
We’ll use Spring initializr web tool to bootstrap our application. Follow the steps below to generate the application :
Go to http://start.spring.io
Select Kotlin in the language section.
Enter Artifact as kotlin-demo
Add Web, JPA, and MySQL dependencies.
Click Generate to generate and download the project.
Once the project is generated, unzip it and import it into your favorite IDE.2. Configure MySQL Database
spring.datasource.url = jdbc:mysql://localhost:3306/demo?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
spring.datasource.username = root
spring.datasource.password = root
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = update
3. Create JPA Entity - Post.kt
Create a new package called model inside com.example.kotlindemo package, and then create a new Kotlin file called Post.kt with the following contents -package com.example.kotlindemo.model
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.validation.constraints.NotBlank
@Entity
data class Post (
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0,
@get: NotBlank
val title: String = "",
@get: NotBlank
val content: String = ""
)
4. Create JPA Repository - PostRepository.kt
package com.example.kotlindemo.repository
import com.example.kotlindemo.model.Post
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface PostRepository : JpaRepository<Post, Long>
5. Create Spring MVC Controller - PostController.kt
Finally, Let’s create the controller end-points for all the CRUD operations on the Post entity.First, create a new package called controller inside com.example.kotlindemo package and then create a new kotlin file called PostController.kt inside controller package with the following contents -
package com.example.kotlindemo.controller
import com.example.kotlindemo.model.Post
import com.example.kotlindemo.repository.PostRepository
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.util.*
import javax.validation.Valid
@RestController
@RequestMapping("/api")
class PostController(private val postRepository: PostRepository) {
@GetMapping("/posts")
fun getAllPosts(): List<Post> =
postRepository.findAll()
@PostMapping("/posts")
fun createNewPost(@Valid @RequestBody post: Post): Post =
postRepository.save(post)
@GetMapping("/posts/{id}")
fun getPostById(@PathVariable(value = "id") postId: Long): ResponseEntity<Post> {
return postRepository.findById(postId).map { post ->
ResponseEntity.ok(post)
}.orElse(ResponseEntity.notFound().build())
}
@PutMapping("/posts/{id}")
fun updatePostById(@PathVariable(value = "id") postId: Long,
@Valid @RequestBody newPost: Post): ResponseEntity<Post> {
return postRepository.findById(postId).map { existingPost ->
val updatedPost: Post = existingPost
.copy(title = newPost.title, content = newPost.content)
ResponseEntity.ok().body(postRepository.save(updatedPost))
}.orElse(ResponseEntity.notFound().build())
}
@DeleteMapping("/posts/{id}")
fun deletePostById(@PathVariable(value = "id") postId: Long): ResponseEntity<Void> {
return postRepository.findById(postId).map { post ->
postRepository.delete(post)
ResponseEntity<Void>(HttpStatus.OK)
}.orElse(ResponseEntity.notFound().build())
}
}
6. Running the Application
mvn spring-boot:run
7. Testing the Rest APIs
- POST /api/posts - Create an Post
curl -i -H "Content-Type: application/json" -X POST \
-d '{"title": "How to learn Kotlin", "content": "Resources to learn Kotlin"}' \
http://localhost:8080/api/posts
# Output
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
{"id":1,"title":"How to learn Kotlin","content":"Resources to learn Kotlin"}
- GET /api/posts - Get all Posts
curl -i -H 'Accept: application/json' http://localhost:8080/api/posts
# Output
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
[{"id":1,"title":"How to learn Kotlin","content":"Resources to learn Kotlin"}]
- Get /api/posts/{id} - Get an Post by id
curl -i -H 'Accept: application/json' http://localhost:8080/api/posts/1
# Output
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
{"id":1,"title":"How to learn Kotlin","content":"Resources to learn Kotlin"}
- PUT /api/posts/{id} - Update an Post
curl -i -H "Content-Type: application/json" -X PUT \
-d '{"id":1,"title":"How to learn Kotlin","content":"Resources to learn Kotlin"}' \
http://localhost:8080/api/posts/1
# Output
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
{"id":1,"title":"How to learn Kotlin","content":"Resources to learn Kotlin"}
- DELETE /api/posts/{id} - Delete an Post
curl -i -X DELETE http://localhost:8080/api/posts/1
# Output
HTTP/1.1 200
Content-Length: 0
Conclusion
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course
Comments
Post a Comment