In this article, I create a sample use Spring Reactive on latest technologies stack: JDK 17, Gradle 7.3 , IntelliJ IDEA 2021.2.3 Ultimate, Spring Boot 2.6.0
, Spring dependency management 1.0.11.RELEASE
.
Check environment
C:Usersdonhu>gradle -v
------------------------------------------------------------
Gradle 7.3
------------------------------------------------------------
Build time: 2021-11-09 20:40:36 UTC
Revision: 96754b8c44399658178a768ac764d727c2addb37
Kotlin: 1.5.31
Groovy: 3.0.9
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM: 17.0.1 (Oracle Corporation 17.0.1+12-LTS-39)
OS: Windows 10 10.0 amd64
C:Usersdonhu>
JDK
C:Usersdonhu>java --version
java 17.0.1 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)
C:Usersdonhu>javac --version
javac 17.0.1
C:Usersdonhu>
File build.gradle
plugins {
id 'org.springframework.boot' version '2.6.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '17'
targetCompatibility = '17'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
}
test {
useJUnitPlatform()
}
File Application.java
packageemployee;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.context.ConfigurableApplicationContext;@SpringBootApplicationpublicclassApplication{publicstaticvoidmain(String[] args){ConfigurableApplicationContext context =SpringApplication.run(Application.class, args);EmployeeClient employeeClient = context.getBean(EmployeeClient.class);System.out.println(">>> Client called: "+ employeeClient.getMessage().block());}}
File application.properties
server.port=8087
File Employee.java
packageemployee;importjava.util.Objects;publicclassEmployee{privateString firstName;privateString lastName;privateString dateOfBirth;publicEmployee(){}publicEmployee(String firstName,String lastName,String dateOfBirth){this.firstName = firstName;this.lastName = lastName;this.dateOfBirth = dateOfBirth;}publicStringgetFirstName(){return firstName;}publicvoidsetFirstName(String firstName){this.firstName = firstName;}publicStringgetLastName(){return lastName;}publicvoidsetLastName(String lastName){this.lastName = lastName;}publicStringgetDateOfBirth(){return dateOfBirth;}publicvoidsetDateOfBirth(String dateOfBirth){this.dateOfBirth = dateOfBirth;}@Overridepublicbooleanequals(Object o){if(this== o)returntrue;if(o ==null||getClass()!= o.getClass())returnfalse;Employee employee =(Employee) o;returnObjects.equals(firstName, employee.firstName)&&Objects.equals(lastName, employee.lastName)&&Objects.equals(dateOfBirth, employee.dateOfBirth);}@OverridepublicinthashCode(){returnObjects.hash(firstName, lastName, dateOfBirth);}}
File EmployeeHandler.java
packageemployee;importorg.springframework.http.MediaType;importorg.springframework.stereotype.Component;importorg.springframework.web.reactive.function.BodyInserters;importorg.springframework.web.reactive.function.server.ServerRequest;importorg.springframework.web.reactive.function.server.ServerResponse;importreactor.core.publisher.Mono;@ComponentpublicclassEmployeeHandler{publicMono<ServerResponse>getInfo(ServerRequest request){returnServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(newEmployee("Vy","Do Nhu","26/08/1987")));}}
File EmployeeRouter.java
packageemployee;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.http.MediaType;importorg.springframework.web.reactive.function.server.RouterFunction;importorg.springframework.web.reactive.function.server.RouterFunctions;importorg.springframework.web.reactive.function.server.ServerResponse;importstaticorg.springframework.web.reactive.function.server.RequestPredicates.GET;importstaticorg.springframework.web.reactive.function.server.RequestPredicates.accept;@Configuration(proxyBeanMethods =false)publicclassEmployeeRouter{@BeanpublicRouterFunction<ServerResponse>route(EmployeeHandler employeeHandler){returnRouterFunctions.route(GET("/employee").and(accept(MediaType.APPLICATION_JSON)), employeeHandler::getInfo);}}
File EmployeeClient.java
packageemployee;importorg.springframework.http.MediaType;importorg.springframework.stereotype.Component;importorg.springframework.web.reactive.function.client.WebClient;importreactor.core.publisher.Mono;@ComponentpublicclassEmployeeClient{privatefinalWebClient client;publicEmployeeClient(WebClient.Builder builder){this.client = builder.baseUrl("http://localhost:8087").build();}publicMono<String>getMessage(){returnthis.client.get().uri("/employee").accept(MediaType.APPLICATION_JSON).retrieve().bodyToMono(Employee.class).map(Employee::toString);}}
Run project
Test RESTful API by Postman
Source code: https://github.com/donhuvy/sample_spring_reactive.git
Nguồn: viblo.asia