Please forgive the point that it is a poor description because it is the first post. : bow_tone1: I recently learned the word "Spring WebFlux", so I'll touch on it through the tutorial.
Spring WebFlux is a web framework provided by Spring and performs non-blocking processing. You can do it. Roughly speaking, I understood that it is a "mechanism that can process efficiently while saving threads".
I would like to try the tutorial on the following page. Building a Reactive RESTful Web Service
Development environment ・ Visual Studio Code ・ AdoptOpenJDK 11.0.5 + 10
Clone the source code locally.
$ git clone https://github.com/spring-guides/gs-reactive-rest-service.git
Cloning into 'gs-reactive-rest-service'...
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 822 (delta 6), reused 14 (delta 5), pack-reused 801
Receiving objects: 100% (822/822), 408.59 KiB | 771.00 KiB/s, done.
Resolving deltas: 100% (553/553), done.
Since it is developed with vscode, open the developed folder. Menu: File> Open> cloned gs-reactive-rest-service folder
Create a new GreetingHandler.java under ʻinitial / src / main / java / hello`.
GreetingHandler.java
package hello;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class GreetingHandler {
public Mono<ServerResponse> hello(ServerRequest request) {
return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromObject("Hello, Spring!"));
}
}
Create a new GreetingRouter.java under ʻinitial / src / main / java / hello`.
GreetingRouter.java
package hello;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
@Configuration
public class GreetingRouter {
@Bean
public RouterFunction<ServerResponse> route(GreetingHandler greetingHandler) {
return RouterFunctions
.route(RequestPredicates.GET("/hello").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), greetingHandler::hello);
}
}
Create a new GreetingWebClient.java under ʻinitial / src / main / java / hello`.
GreetingWebClient
package hello;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
public class GreetingWebClient {
private WebClient client = WebClient.create("http://localhost:8080");
private Mono<ClientResponse> result = client.get()
.uri("/hello")
.accept(MediaType.TEXT_PLAIN)
.exchange();
public String getResult() {
return ">> result = " + result.flatMap(res -> res.bodyToMono(String.class)).block();
}
}
ʻCreate a new Application.java under initial / src / main / java / hello`.
Application.java
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
GreetingWebClient gwc = new GreetingWebClient();
System.out.println(gwc.getResult());
}
}
Right-click on Application.java and select Run and you will see the following message in the terminal:
>> result = Hello, Spring WebFlux!
Similarly, if you open http: // localhost: 8080 / hello in your browser, you will see the same message.
Thank you for reading to the end. I'm interested in programming similar to myself, but I hope it helps people who have no clue.
Recommended Posts