Spring boot

Spring boot webclient example

Spring boot WebClient with Spring WebFlux

WebClient makes the Spring WebFlux create non-blocking Http request.

If you add Spring WebFlux on your classpath, WebClient will be the default choice to call remote REST services. When compared to RestTemplate, this client has a more functional feel and is fully reactive.

Autoconfiguration in Spring Boot creates and pre-configures a WebClient Builder with default setting; it is strongly advised to inject it in your components and use it to create WebClient instances.

Internally WebClient delegates to an HTTP client library. By default, it uses Reactor Netty, there is built-in support for the Jetty reactive HttpClient, and others can be plugged in through a ClientHttpConnector

Starter Configuration (pom.xml)

Spring boot web flux comes with WebClient in Java dependency, Just Autowire it in your application

	<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.6.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.candidjava</groupId>
	<artifactId>Spring_Boot_WebClient</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Spring_Boot_WebClient Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webflux</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>org.projectreactor</groupId>
			<artifactId>reactor-spring</artifactId>
			<version>1.0.1.RELEASE</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
		<finalName>Spring_Boot_WebClient</finalName>
	</build>
</project>

WebClient Builder (SpringWebClientApplication.java)

Create a Spring bean for WebClient using WebClient Builder.

package com.candidjava.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.client.WebClient;

@SpringBootApplication
public class SpringWebClientApplication {
 @Bean
 public WebClient.Builder getWebClientBuilder() {
  return WebClient.builder();
 }

 public static void main(String[] args) {
  SpringApplication.run(SpringWebClientApplication.class, args);
 }

}

Test WebClient (TodosService.java)

Test your Web Client configuration by Autowiring it in the application.

package com.candidjava.springboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

import com.candidjava.springboot.models.Todos;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
public class TodosService {

 @Autowired
 private WebClient.Builder webClientBuilder;

 private static final String baseURL = "https://jsonplaceholder.typicode.com/todos";

 public Flux<Todos> getAllTodos() {
  Flux<Todos> todos = webClientBuilder.build().get()
   .uri(baseURL).retrieve().bodyToFlux(Todos.class);
  return todos;

 }

 public Mono<Todos> getTodosById(String id) {
  Mono<Todos> todo = webClientBuilder.build().get()
   .uri(baseURL + "/" + id).retrieve().bodyToMono(Todos.class);
  return todo;
 }

}

TodosController.java

package com.candidjava.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.candidjava.springboot.models.Todos;
import com.candidjava.springboot.service.TodosService;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping(value = "/todos")
public class TodosController {
 @Autowired
 TodosService service;

 @GetMapping("/getAll")
 public Flux<Todos> get() {
  return service.getAllTodos();
 }

 @GetMapping("/get/{id}")
 public Mono<Todos> getById(@PathVariable("id") String id) {
  return service.getTodosById(id);
 }

}

Todos.java

package package com.candidjava.springboot.models;

public class Todos {
 private String id;
 private String userId;
 private String title;
 private String completed;

 public Todos() {}

 public String getUserId() {
  return userId;
 }

 public void setUserId(String userId) {
  this.userId = userId;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public String getCompleted() {
  return completed;
 }

 public void setCompleted(String completed) {
  this.completed = completed;
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

}

Download

Download source code from my github account Click here