nic*_*net 4 java kotlin spring-webflux spring-webclient
我正在使用 Spring WebFlux 接受请求并使用相同的请求来调用另一个服务。但我不确定如何添加查询参数。这是我的代码
@RequestMapping(value= ["/ptta-service/**"])
suspend fun executeService(request: ServerHttpRequest): ServerHttpResponse {
val requestedPath = if (request.path.toString().length > 13) "" else request.path.toString().substring(13)
return WebClient.create(this.dataServiceUrl)
.method(request.method ?: HttpMethod.GET)
.uri(requestedPath)
.header("Accept", "application/json, text/plain, */*")
.body(BodyInserters.fromValue(request.body))
.retrieve()
.awaitBody()
// But how about query parameters??? How to add those
}
Run Code Online (Sandbox Code Playgroud)
尽管代码在 Kotlin 中,但 Java 也会有所帮助。
您可以在uri 中使用 lambda 表达式添加查询参数,有关更多信息,请参阅WebClient.UriSpec
return WebClient.create(this.dataServiceUrl)
.method(request.method ?: HttpMethod.GET)
.uri(builder -> builder.path(requestedPath).queryParam("q", "12").build())
.header("Accept", "application/json, text/plain, */*")
.body(BodyInserters.fromValue(request.body))
.retrieve()
.awaitBody()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9419 次 |
| 最近记录: |