Leonardo Borges
01/09/2023, 12:20 PMHttpServletResponse
within a suspend function.
The approach we are using now is not good, so we would like to find a proper solution:
@GetMapping("/users")
suspend fun getUsers(//...parameters, response: HttpServletResponse): List<UsersResource> {
val users = userService.searchBy(//parameters)
response.addPaginationHeaders(users.size)
return users
}
For instance, addPaginationHeaders
will be changed only for example purposes, as below:
internal fun HttpServletResponse.addPaginationHeaders(size: Int) {
val uri = ServletUriComponentBuilder.fromCurrentRequest().toUriString()
setHeaders("X-Count", size)
//...other params
}
So, suspend function doesn't work (throws java.lang.IllegalStateException: No current ServletRequestAttributes
).
Instead, we have to place the function body within a runBlocking
to make it work.
suspend fun getUsers(\\...parameters, response: HttpServletResponse) = runBlocking {
//same code as above
}
Has anyone experienced this issue? Is there any solution besides runBlocking
?sdeleuze
01/09/2023, 1:22 PMefemoney
01/09/2023, 2:14 PMuserService.searchBy
returnsefemoney
01/09/2023, 2:19 PMsdeleuze
01/09/2023, 2:21 PMsdeleuze
01/09/2023, 2:21 PMMono
to see if that's Coroutines specific or not.sdeleuze
01/13/2023, 11:55 AMLeonardo Borges
01/17/2023, 5:12 PMLeonardo Borges
01/17/2023, 5:13 PM