1
1
.
.
2
2
.
.
3
3
C
C
l
l
i
i
e
e
n
n
t
t
-
-
F
F
l
l
u
u
x
x
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to use WebClient Class to call Remote Server Endpoints that return Person(s).
This tutorial uses bodyToFlux() to retrieve Data in a List<Person>.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller, @RequestMapping and Tomcat Server
Web
Spring Reactive Web
Enables WebClient
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: controller_returns_text_json (add Spring Boot Starters from the table)
Create Package: entities (inside main package)
Create Interface: Person.java (inside package entities)
Create Package: services (inside main package)
Create Class: PersonService.java (inside controllers package)
Create Package: runners (inside main package)
Create Class: MyCommandLineRunner.java (inside runners package)
Person.java
package com.ivoronline.springboot_webclient_server.entities;
public class Person {
public Integer id;
public String name;
public Integer age;
}
Person
{"id":1,"name":"Susan","age":20}
http://localhost:8085/GetPerson
PersonService
MyCommandLineRunner
Server
http://localhost:8085/GetPersons
[{"id":2,"name":"John","age":30},
{"id":3,"name":"Bill","age":40}]
PersonService.java
package com.ivoronline.springboot_webclient_flux.services;
import com.ivoronline.springboot_webclient_flux.entities.Person;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import java.time.Duration;
import java.util.List;
@Component
public class PersonService {
//===============================================================
// GET PERSON
//===============================================================
public void getPerson() {
//GET PERSON FROM SERVER
Person person = WebClient.create("http://localhost:8085")
.get()
.uri("/GetPerson")
.retrieve()
.bodyToMono(Person.class)
.block(Duration.ofSeconds(3));
//DISPLAY PERSON
System.out.println("getPerson()");
System.out.println(person.id + " " + person.name+ " " + person.age);
}
//===============================================================
// GET PERSONS
//===============================================================
public void getPersons() {
//GET PERSON FROM SERVER
List<Person> persons = WebClient.create("http://localhost:8085")
.get()
.uri("/GetPersons")
.retrieve()
.bodyToFlux(Person.class)
.collectList()
.block(Duration.ofSeconds(3));
//DISPLAY PERSONS
System.out.println("getPersons()");
for (Person person : persons) {
System.out.println(person.id + " " + person.name+ " " + person.age);
}
}
}
MyCommandLineRunner.java
package com.ivoronline.springboot_webclient.runners;
import com.ivoronline.springboot_webclient.services.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Autowired private PersonService personService;
@Override
public void run(String... args) throws Exception {
personService.getPerson();
personService.getPersons();
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
Console
getPerson()
1 Susan 30
getPersons()
2 John 40
3 Bill 50
Application Structure
pom.xml
<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>
</dependencies>