Hello! I'm having some issues when trying to add custom headers to a
HttpServletResponse
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
?