Search code examples
spring-boot

How to send request parameters with the new Spring Boot 3.2 RestClient?


I have looked at the examples and the javadoc but I can't find an example anywhere.

I am migrating from WebClient to the new RestClient.

I have looked online in the obvious places.

I am trying to send 5 parameters and a path variable.


Solution

  • Example of using RestClient:

    RestClient restClient = RestClient.builder()
          .baseUrl("https://test.com/api")
          .build();
    
    String pathVariable = "pathVariable";
    ResponseEntity<String> response = restClient.get()
        .uri(uriBuilder -> uriBuilder
             .path("/" + pathVariable)
             .queryParam("param1", "value1")
             .queryParam("param2", "value2")
             .queryParam("param3", "value3")
             .queryParam("param4", "value4")
             .queryParam("param5", "value5")
             .build())
        .header("Content-Type", "application/json")
        .retrieve()
        .toEntity(String.class)
        .block();