Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin SpringWebFlux中代理请求的反应式方法_Kotlin_Spring Webflux - Fatal编程技术网

Kotlin SpringWebFlux中代理请求的反应式方法

Kotlin SpringWebFlux中代理请求的反应式方法,kotlin,spring-webflux,Kotlin,Spring Webflux,我需要通过添加一些额外的头(通常是OAuth令牌),将请求转发到不同的端点 我试着在下面工作一个代理请求 fun proxy(request: ServerRequest, url:String, customHeaders: HttpHeaders = HttpHeaders.EMPTY): Mono<ServerResponse> { val modifiedHeaders = getHeadersWithoutOrigin(request, customHe

我需要通过添加一些额外的头(通常是OAuth令牌),将请求转发到不同的端点

我试着在下面工作一个代理请求

 fun proxy(request: ServerRequest, url:String, customHeaders: HttpHeaders = HttpHeaders.EMPTY): Mono<ServerResponse> {

        val modifiedHeaders = getHeadersWithoutOrigin(request, customHeaders)

        var webClient = clientBuilder.method(request.method()!!)
                .uri(url)
        modifiedHeaders.forEach{
            val list = it.value.iterator().asSequence().toList()
            val ar:Array<String> = list.toTypedArray()
            webClient.header(it.key, *ar)
        }
        return webClient
                .body(request.bodyToMono(), DataBuffer::class.java).exchange()
                .flatMap { clientResponse ->
                    ServerResponse.status(clientResponse.statusCode())
                            .headers{
                                it.addAll(clientResponse.headers().asHttpHeaders())
                            }
                            .body(clientResponse.bodyToMono(), DataBuffer::class.java)
                }
    }
fun代理(请求:ServerRequest,url:String,customHeaders:HttpHeaders=HttpHeaders.EMPTY):Mono{
val modifiedHeaders=getHeadersWithoutOrigin(请求、自定义标题)
var webClient=clientBuilder.method(request.method()!!)
.uri(url)
修改后的头{
val list=it.value.iterator().asSequence().toList()
val ar:Array=list.toTypedArray()
webClient.header(it.key,*ar)
}
返回网络客户端
.body(request.bodyToMono(),DataBuffer::class.java).exchange()
.flatMap{clientResponse->
ServerResponse.status(clientResponse.statusCode())
.标题{
it.addAll(clientResponse.headers().asHttpHeaders())
}
.body(clientResponse.bodytomino(),DataBuffer::class.java)
}
}
传入的请求总是命中我的服务器上的一个代理端点,并且在头中包含目标url。在服务器上,我读取目标url并添加OAuth令牌,然后将请求转发到目标url。在这个场景中,我不想解析响应体。在下游发送响应


什么是反应式方法?

可能尝试使用
BodyExtractor
类将正文提取到数据缓冲区,然后将此数据缓冲区传递到新的webclient。您不应该在每次请求时都创建一个新的客户端,这很昂贵。@Thomas,您能用一个例子解释一下吗?您可以尝试使用
BodyExtractor
类将正文提取到数据缓冲区,然后将此数据缓冲区传递到新的webclient。您不应该在每个请求上创建一个新的客户机,这很昂贵。@Thomas,您能举个例子解释一下吗