Hello everyone. I’m learning Spring boot with kotl...
# spring
d
Hello everyone. I’m learning Spring boot with kotlin’s coroutines, anyone (that knows more that me about coroutines/reactivity) could confirm that my code is non blocking ? I have this method in my controller:
Copy code
@PostMapping("/posts")
    suspend fun create(@Valid @RequestBody postDto: PostDto): ResponseEntity<PostDto> {
        val response = postService.create(postDto)
        return if (response != null) ResponseEntity.ok(response)
        else ResponseEntity.notFound().build()
    }
then:
Copy code
@Service
class PostService(val repository: PostRepository) {

    ...
    suspend fun create(postDto: PostDto): PostDto? =
            repository.create(postDto)
and in the repo I have :
Copy code
suspend fun create(postDto: PostDto): PostDto? =
            databaseClient.sql("INSERT INTO posts (title, description) VALUES (:title, :description)")
                    .filter { statement, _ -> statement.returnGeneratedValues("id").execute() }
                    .bind("title", postDto.title)
                    .bind("description", postDto.description)
                    .fetch()
                    .first()
                    .map { row ->
                        val id = row["id"] as Long
                        val postEntity = postDto.toEntity().copy(id = id)
                        postEntity.toDto()
                    }
                    .awaitSingleOrNull()
In the controller method, I’m not sure
ResponseEntity<PostDto>
is non blocking. Anyone knows ? I’m not sure my exceptionHandler nor my validator are non blocking. The code is available on this repo here: https://github.com/danygiguere/spring-boot-3-reactive-with-kotlin-coroutines
j
You don't need suspend in the ExceptionHandler since it doesn't contain any blocking I/o operations Blocking I/O Operations: When reading from or writing to files, network sockets, or databases synchronously, the code typically blocks until the operation completes
d
Thanks @Johan I get it. For what you’re saying it seems it’s working just like javascript async/await.
j
Yes I would say it's similar to async 👍