Reputation: 11
How can I call the method which return Mono<> and use it to call web method itself?
@Component
class SampleWebFilter(private val sampleService: SampleService) : WebFilter {
override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
val accessToken =
exchange.request.headers["Authorization"]?.firstOrNull()
?: throw IllegalArgumentException("Access token must not be empty")
val res = sampleService.authorize(accessToken)
val id = res.block()?.userId
exchange.attributes["UserId"] = userId
return chain.filter(exchange)
}
}
@Component
interface SampleService {
@GET("/user")
fun authorize(accessToken): Mono<User>
}
the code above throw exception
block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-2
I know we shouldn't block the thread on netty but how can I use id from the SampleService to call web method.
Thanks in advance.
Upvotes: 0
Views: 561
Reputation: 11
@Component
class SampleWebFilter(private val sampleService: SampleService) : WebFilter {
override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
val accessToken =
exchange.request.headers["Authorization"]?.firstOrNull()
?: throw IllegalArgumentException("Access token must not be empty")
val res = sampleService.authorize(accessToken)
return res.doOnNext {
exchange.attributes["UserId"] = userId
}
.then(chain.filter(exchange))
}}
@Component
interface SampleService {
@GET("/user")
fun authorize(accessToken): Mono<User>
}
I solved the problem writing like above.
Upvotes: 1