Spring WebFlux

Spring WebFlux で REST API を作成し、 GET リクエストに対して json を返します。

環境

1. Spring Boot WebFlux の依存関係を追加する

pom.xml に以下を追加します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
</dependencies>

追加後の内容は以下のようになります。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.kujilabo</groupId>
<artifactId>spring-002</artifactId>
<version>1.0.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
</dependencies>
</project>

pom.xml を右クリックし、 Maven -> Reimport をクリックしインポートしておきます。

2. RestController を作成する

src/main/java を右クリックし、 New -> Package をクリックし、パッケージを作成します。ここでは com.kujilabo としました。
src/main/java/com.kujilabo を右クリックし、 New -> Java class をクリックし、 Application クラスを作成します。
コードは以下のとおりです。簡略化のため、ルーティングもこのクラスに含めます。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.kujilabo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
import static org.springframework.web.reactive.function.server.ServerResponse.notFound;

@SpringBootApplication
@EnableWebFlux
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

public Mono<Map<String, String>> mono() {
Map<String, String> map = new HashMap<>();
map.put("a", "b");
return Mono.just(map);
}

@Bean
public RouterFunction<ServerResponse> routerFunction() {
return RouterFunctions
.route(GET("/mono").and(accept(MediaType.APPLICATION_JSON)),
request -> mono()
.flatMap(x -> ok().contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromObject(x)))
.switchIfEmpty(notFound().build()));
}
}