Close

Spring 5 - WebClient Example

[Last Updated: Nov 3, 2017]

The spring-webflux module includes a non-blocking, reactive client for HTTP requests with Reactive Streams back pressure.

WebClient interface is the main entry point for initiating web requests on the client side.

Let's see an example to learn how to to use it.

Example

Server Application

Like our web-flux getting started example, we are going to create annotation based controller for our server application first.

@Controller
public class MyController {
  @GetMapping("/")
  @ResponseBody
  public Publisher<String> handler() {
      return Mono.just("Hello world!");
  }
}

JavaConfig and main class

@SpringBootApplication
@EnableWebFlux
public class ExampleApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(ExampleApplication.class);
    }
}

To try examples, run spring-boot maven plugin (configured in pom.xml of example project below):

mvn spring-boot:run

Client Application using WebClient

public class WebClientExample {
  public static void main(String[] args) throws InterruptedException {
      WebClient webClient = WebClient.create("http://localhost:8080");
      Mono<String> result = webClient.get()
                                     .retrieve()
                                     .bodyToMono(String.class);
      String response = result.block();
      System.out.println(response);
  }
}

The method bodyToMono() extracts the body to a Mono instance. The method Mono.block() subscribes to this Mono instance and blocks until the response is received.

Output

Hello world!

The Netty logs have been filtered from above output. Netty is the default ClientHttpConnector.

Getting Response Without Blocking

Following example shows how to use Mono to get the response without blocking the request thread:

public class WebClientExample2 {
  public static void main(String[] args) throws InterruptedException {
      WebClient webClient = WebClient.create("http://localhost:8080");
      Mono<String> result = webClient.get()
                                     .retrieve()
                                     .bodyToMono(String.class);
      result.subscribe(WebClientExample2::handleResponse);
      System.out.println("After subscribe");
      //wait for a while for the response
      Thread.sleep(1000);
  }

  private static void handleResponse(String s) {
      System.out.println("handle response");
      System.out.println(s);
  }
}

Output

After subscribe
handle response
Hello world!

Note that, Mono.subscribe() returns immediately (as opposed to block() method which waits for the full response)

Example Project

Dependencies and Technologies Used:

  • Spring Boot 2.0.0.RELEASE
    Corresponding Spring Version 5.0.4.RELEASE
  • spring-boot-starter-webflux : Starter for building WebFlux applications using Spring Framework's Reactive Web support.
  • JDK 1.8
  • Maven 3.3.9

Spring WebClient Example Select All Download
  • web-client-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • WebClientExample2.java

    See Also