Try the Spring WebFlux tutorial

Introduction

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.

What is WebFlux

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".

What to do this time

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

1. Preparation

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

2. Create a WebFlux Handler.

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!"));
  }
}

3. Make a Router.

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);
  }
}

4. Create a Web Client.

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();
  }
}

5. Allow the app to run.

ʻ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());
  }
}

6. Try it.

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.

Finally

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

Try the Spring WebFlux tutorial
Let's try the S2Struts tutorial (# 3_180425)
Let's try the S2Struts tutorial (# 5_180526)
Let's try the S2Struts tutorial (# 4_180505)
Let's try the S2Struts tutorial (# 1_180423)
Let's try the S2Struts tutorial (# 2_180424)
Let's try the S2Struts tutorial (# 0_yymmdd)
[Tutorial] Spring Batch
Try calling the CORBA service from Spring (Java)
Try using Spring JDBC
Try running Spring Cloud Config for the time being
Try Spring Boot from 0 to 100.
Spring Boot tutorial task schedule
I tried the Docker tutorial!
Try hitting the zip code search API with Spring Boot
Try implementing a WebFlux session
I tried the VueJS tutorial!
Try using Spring Boot Security
Try implementing a WebFlux filter
Try Spring Boot on Mac
Spring Boot for the first time
Spring Boot Tutorial Using Spring Security Authentication
Memo after the first Spring project-MVC-
Spring Cloud Config Embedding the Config Server
Try using the messaging system Pulsar
Try running Spring Boot on Kubernetes
Follow the Datomic tutorial with Datascript
Spring AOP for the first time
Memo after the first Spring project-Database-
Filter the result of BindingResult [Spring]