Spring WebClient with Spring Webflux RestAPIs | SpringBoot 2

In the tutorial, JavaSampleApproach will introduce new client side Spring WebClient that offers a fully non-blocking and reactive alternative to the RestTemplate.

Related posts:
SpringBoot WebFlux Functional RestAPIs
SpringBoot WebFlux Annotation-based RestAPIs
How to use Spring RestTemplate client for consuming Restful WebService
SpringBoot WebFlux Test

I. Technologies

– Java: 1.8
– Maven: 3.3.9
– Spring Tool Suite: Version 3.9.0.RELEASE
– Spring Boot: 2.0.0.M4
– Spring Boot Starter Webflux

II. Spring WebClient

As mentioned above, reactive WebClient is an alternative solution to the RestTemplate for fully non-blocking and reactive.
If we have Spring WebFlux on classpath, we can use WebClient to work with remote RestAPIs.

III. Practice

In the tutorial, we create 2 SpringBoot projects for working {SpringWebClient, SpringWebFluxFunction-Server}:

spring webclient flux server- projects structure

Step do to:
– Create SpringBoot projects
– Create data model
– Implement Spring WebFlux server
– Implement Spring WebClient
– Run and check results

1. Create SpringBoot projects

Using SpringToolSuite, create 2 SpringBoot projects {SpringWebClient, SpringWebFluxFunction-Server} with spring-boot-starter-webflux dependency:

2. Create data model

Create data model Customer for 2 projects:

3. Implement Spring WebFlux server

Refer at: SpringBoot WebFlux Functional RestAPIs

3.1 Implement Customer repository

– Create a interface CustomerRepository:

– Implement CustomerRepositoryImpl:

3.2 Implement WebFlux functional

– define RouterFunction:

– Implement CustomerHandler:

4. Implement Spring WebClient
4.1 Implement WebClient transporter

– Define interface WebClientTransport:

– Implement WebClientTransportImpl:

4.2 Implement WebClient Application

– Open application.properties, configure: server.port=8081

5. Run and check results

Build and Run the SpringWebFluxFunction-Server project then SpringWebClient project by commandlines {mvn clean install, mvn spring-boot:run}.

-> Console logs from SpringWebClient:

IV. Sourcecode


Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *