EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot Webclient
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

Spring Boot Webclient

Spring Boot WebClient

Introduction to Spring Boot WebClient

Spring boot WebClient is basically part of the reactive framework which was used to construct the non-blocking and reactive web-based application. WebClient is part of the spring MVC project, and it will allow communication with http servers; after releasing spring 5, the web client is best and recommended for client communication. In addition, WebClient is a component that was used to make calls with other services.

What is a Spring Boot WebClient?

  • WebClient is used to build web-based and reactive applications. WebClient is supporting the web request, which was non-blocking.
  • From spring 5, spring boot used the spring web framework, which was spring webflux.
  • Spring boot WebClient is designed with APIs of spring boot MVC, but it is also used to add support for the design of non-blocking.
  • Using spring boot webflux, we can design asynchronous types of web applications by using functional API’s and reactive streams for better support of scaling and concurrency.
  • Spring boot WebClient supports the synchronous as well as asynchronous types of web applications.
  • Also, it will support making the synchronous and asynchronous http request by using API of functional fluent, which was interacting directly with our existing spring configuration.
  • Using WebClient, we can send get and post requests to the API of WebClient.
  • To make the request with WebClient, we need to add dependency in the pom.xml file of our application.
  • We need to add spring boot starter webflux dependency on the pom.xml file.
  • After adding the spring boot starter webflux dependency, we do not need to add webflux and reactor-netty dependency; it will configure automatically while adding the webflux dependency in our project.
  • Spring boot WebClient API is used on top of the existing http client library. WebClient is mostly using the reactor netty, but we can also use the apache reactive component and http client, or we can also integrate other custom connectors.
  • After configuring WebClient, we can send the get request to the WebClient.

Spring Boot WebClient Dependencies

  • To develop the application of WebClient by using spring boot, we need to add spring boot starter webflux dependency in the pom.xml file.
  • While developing a project with spring boot maven, we need to add the following dependency in the pom.xml file.

Building Spring Boot WebClient project with maven –

<dependency>   -- Start of dependency tag.
<groupId>org.springframework.boot</groupId>   -- Start and end of groupId tag.
<artifactId>spring-boot-starter-webflux</artifactId>  -- Start and end of artifactId tag.
</dependency>    -- End of dependency tag.
<dependency>   -- Start of dependency tag.
<groupId>io.projectreactor</groupId>  -- Start and end of groupId tag.
<artifactId>reactor-test</artifactId>   -- Start and end of artifactId tag.
<scope>test</scope>
</dependency>   -- End of dependency tag.
<dependency>    -- Start of dependency tag.
<groupId>org.springframework.boot</groupId>   -- Start and end of groupId tag.
<artifactId>spring-boot-starter-web</artifactId>    -- Start and end of artifactId tag.
</dependency>    -- End of dependency tag.
<dependency>   -- Start of dependency tag.
<groupId>org.springframework.experimental</groupId>  -- Start and end of groupId tag.
<artifactId>spring-native</artifactId>    -- Start and end of artifactId tag.
<version>${spring-native.version}</version>
</dependency>    -- End of dependency tag.
<dependency>   -- Start of dependency tag.
<groupId>io.projectreactor</groupId>    -- Start and end of groupId tag.
<artifactId>reactor-test</artifactId>     -- Start and end of artifactId tag.
<scope>test</scope>
</dependency>   -- End of dependency tag.

  • After adding webflux dependencies of spring boot, other dependencies are automatically added. We do not need to add another dependency separately after adding the webflux dependency.
  • While developing a project with spring boot gradle, we need to add the following dependency in the build.gradle file.

Building WebClient project with gradle –

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

dependencies
{
compile 'org.springframework.boot: spring-boot-starter-webflux'
}

Working Spring Boot WebClient

  • While working with WebClient, we need to follow the below steps.
  • First, we need to create a WebClient instance. To create the WebClient instance, first, we need to create a WebClient object. After creating the object then we need to initiate the WebClient instance. Finally, after initiating the instance, we need to build the client by using the builder class.
  • After creating an instance, define a method for preparing the request; after defining the method, the next step is to define the URL for preparing the request.
  • After defining URL next step is to define the body for preparing the request. After defining the body next step is to define headers for preparing requests.
  • After creating all the setup final step is sending requests and getting responses from WebClient.

Create and configure WebClient

The below examples show to create and configure WebClient as follows.

  • Create a project template using spring initializer and give a name to the project –

In the below step, we have provided the project group name, artifact name, package, and description.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)

Group – com.example
Artifact name – spring-boot-webclient-example-master
Name – spring-boot-webclient-example-master
Description - Project for spring-boot-webclient-example-master
Spring boot – 2.5.5
Project – Maven project
Package name - com.example. spring-boot-webclient-example-master
Packaging – Jar
Java – 11
Dependencies – spring native, spring web, spring reactive web.

spring boot webclient output 1

  • After generating project extract files and open this project by using spring tool suite –

spring boot webclient output 2

  • After opening the project using the spring tool suite, check the project and its files –

spring boot webclient output 3

  • Add dependency packages –

spring boot webclient output 4

  • Configure application.properties file –

Code –

server.port=8080
logging.level.org.springframework = DEBUG
server.servlet.context-path = /emp_mgmt
spring.codec.max-in-memory-size = 2MB

output 5

  • Configure webflux client –

Code –

@Configuration
@EnableWebFlux
public class WebFluxConfig implements WebFluxConfigurer
{
Logger log = LoggerFactory.getLogger (WebFluxConfig. Class);
@Bean
public WebClient getWebClient ()
{
return WebClient.builder ()
.baseUrl ("http://localhost:8080")
.defaultHeader (HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build ();
}
}

output 6

Spring Boot WebClient Example

Below is the example of WebClient as follows.

  • Retrieve and exchange the data from WebClient –

Code –

@Service
public class EmployeeService
{
@Autowired
WebClient wc;
public Flux<Employee> findAll()
{
return wc.get()
.uri ("/get/all")
.retrieve ()
.bodyToFlux (Employee.class)
.timeout (Duration.ofMillis (10_000));
}
}

output 7

  • Create rest controller –

Code –

@RestController
@RequestMapping("/emp")
public class EmployeeController
{
@Autowired
private EmployeeService empSer;
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@ResponseStatus (HttpStatus.OK)
public Flux<Employee> findAll() {
return empSer.findAll ();
}
}

output 8

  • Run and test the application –

output 9

Conclusion

WebClient is used to build web-based and reactive applications. WebClient is supporting web requests which were non-blocking. WebClient is basically part of a reactive framework that was used to construct non-blocking and reactive web-based applications.

Recommended Articles

This is a guide to Spring Boot WebClient. Here we discuss the example to create and configure Spring Boot WebClient along with the codes. You may also have a look at the following articles to learn more –

  1. Spring Boot Maven
  2. Spring Boot Initializr
  3. Spring Boot Batch
  4. Spring Boot Change Port
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more