LoginSignup
6
3

More than 3 years have passed since last update.

Spring WebFluxのチュートリアルをやってみる

Last updated at Posted at 2019-12-20

はじめに

初投稿ということで拙い記載となっている点はご容赦ください。:bow_tone1:
『Spring WebFlux』というワードを最近知ったので、チュートリアルを通して触れてみようと思います。

WebFluxとは

Spring WebFluxはSpringが提供するWebフレームワークであり、ノンブロッキングな処理を行うことができます。
ざっくりですが、『スレッドを節約しつつ、効率よく処理ができる仕組み』だと理解しました。

今回やること

以下のページにあるチュートリアルをやってみたいと思います。
Building a Reactive RESTful Web Service

開発環境
・Visual Studio Code
・AdoptOpenJDK 11.0.5+10

1. 準備

ソースコードを自分のローカルにcloneします。

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

vscodeで開発するので、開発したフォルダを開きます。
メニュー:ファイル>開く>cloneしたgs-reactive-rest-serviceフォルダ

2. WebFlux Handlerをつくる。

initial/src/main/java/hello 配下にGreetingHandler.javaを新規作成します。

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. Routerをつくる。

initial/src/main/java/hello 配下にGreetingRouter.javaを新規作成します。

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. WebClientをつくる。

initial/src/main/java/hello 配下にGreetingWebClient.javaを新規作成します。

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. アプリを実行できるようにする。

initial/src/main/java/hello 配下にApplication.javaを新規作成します。

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. 実行してみる。

Application.javaを右クリックして、Runを選択してみると以下のメッセージがターミナルに表示されます。
>> result = Hello, Spring WebFlux!
同様に、http://localhost:8080/hello をブラウザで開いてみても同じメッセージが表示されます。

最後に

最後まで読んでいただきありがとうございます。
自分と同じようなプログラミングに興味はあるけど、とっかかりがない人の一助になればいいなと思います。

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3