14

I am using Spring WebFlux where I am taking request and using same request to call another service. But I am not sure how to add query parameters. This is my code

@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
}

Though code is in Kotlin, Java will help as well.

nicholasnet
  • 2,117
  • 2
  • 24
  • 46

1 Answers1

21

You can add the query parameters using lambda expression in uri, for more information see 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()
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • 4
    Thank you solution was `.uri { builder -> builder.path(requestedPath).queryParams(request.queryParams).build() }` in Kotlin. But you suggestion was correct. – nicholasnet Jan 26 '20 at 15:19
  • How to use Webclient this if GET method has parameter with @Modalattribute ? – Joker Feb 13 '22 at 22:27