Skip to content

Commit 9f21415

Browse files
committed
add: functional endpoint and annotated controller
1 parent b3c7a69 commit 9f21415

9 files changed

Lines changed: 209 additions & 55 deletions

File tree

‎spring-webflux/pom.xml‎

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,50 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
6-
<groupId>com.baeldung</groupId>
7-
<artifactId>spring-webflux</artifactId>
8-
<version>0.0.1-SNAPSHOT</version>
9-
<packaging>jar</packaging>
10-
11-
<name>spring-webflux</name>
12-
<description>Spring Webflux sample project about filters</description>
13-
14-
<parent>
15-
<groupId>org.springframework.boot</groupId>
16-
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>2.0.0.RELEASE</version>
18-
<relativePath/> <!-- lookup parent from repository -->
19-
</parent>
20-
21-
<properties>
22-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23-
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24-
<java.version>1.8</java.version>
25-
</properties>
26-
27-
<dependencies>
28-
<dependency>
29-
<groupId>org.springframework.boot</groupId>
30-
<artifactId>spring-boot-starter-webflux</artifactId>
31-
</dependency>
32-
33-
<dependency>
34-
<groupId>org.springframework.boot</groupId>
35-
<artifactId>spring-boot-devtools</artifactId>
36-
<scope>runtime</scope>
37-
</dependency>
38-
<dependency>
39-
<groupId>org.springframework.boot</groupId>
40-
<artifactId>spring-boot-starter-test</artifactId>
41-
<scope>test</scope>
42-
</dependency>
43-
<dependency>
44-
<groupId>io.projectreactor</groupId>
45-
<artifactId>reactor-test</artifactId>
46-
<scope>test</scope>
47-
</dependency>
48-
</dependencies>
49-
50-
<build>
51-
<plugins>
52-
<plugin>
53-
<groupId>org.springframework.boot</groupId>
54-
<artifactId>spring-boot-maven-plugin</artifactId>
55-
</plugin>
56-
</plugins>
57-
</build>
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>spring-webflux</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-webflux</name>
12+
<description>Spring Webflux sample project about filters</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.0.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-webflux</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-maven-plugin</artifactId>
45+
</plugin>
46+
</plugins>
47+
</build>
5848

5949

6050
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.springwebflux.controller;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RestController;
6+
import reactor.core.publisher.Mono;
7+
8+
@RestController
9+
public class UserController {
10+
11+
@GetMapping(path = "/users/{name}")
12+
public Mono<String> getName(@PathVariable String name) {
13+
return Mono.just(name);
14+
}
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.springwebflux.filter;
2+
3+
import org.springframework.web.reactive.function.server.HandlerFilterFunction;
4+
import org.springframework.web.reactive.function.server.HandlerFunction;
5+
import org.springframework.web.reactive.function.server.ServerRequest;
6+
import org.springframework.web.reactive.function.server.ServerResponse;
7+
import reactor.core.publisher.Mono;
8+
9+
import static org.springframework.http.HttpStatus.FORBIDDEN;
10+
11+
public class ExampleHandlerFilterFunction implements HandlerFilterFunction {
12+
13+
@Override
14+
public Mono filter(ServerRequest serverRequest, HandlerFunction handlerFunction) {
15+
if (serverRequest.pathVariable("name").equalsIgnoreCase("test")) {
16+
return ServerResponse.status(FORBIDDEN).build();
17+
}
18+
return handlerFunction.handle(serverRequest);
19+
}
20+
21+
}
22+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.springwebflux.filter;
2+
3+
import org.springframework.stereotype.Component;
4+
import org.springframework.web.server.ServerWebExchange;
5+
import org.springframework.web.server.WebFilter;
6+
import org.springframework.web.server.WebFilterChain;
7+
import reactor.core.publisher.Mono;
8+
9+
@Component
10+
public class ExampleWebFilter implements WebFilter {
11+
12+
@Override
13+
public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
14+
serverWebExchange.getResponse().getHeaders().add("web-filter", "web-filter-test");
15+
return webFilterChain.filter(serverWebExchange);
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.springwebflux.handler;
2+
3+
import org.springframework.stereotype.Component;
4+
import org.springframework.web.reactive.function.server.ServerRequest;
5+
import org.springframework.web.reactive.function.server.ServerResponse;
6+
import reactor.core.publisher.Mono;
7+
8+
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
9+
10+
@Component
11+
public class PlayerHandler {
12+
13+
public Mono<ServerResponse> getName(ServerRequest request) {
14+
Mono<String> name = Mono.just(request.pathVariable("name"));
15+
return ok().body(name, String.class);
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.springwebflux.router;
2+
3+
import com.baeldung.springwebflux.filter.ExampleHandlerFilterFunction;
4+
import com.baeldung.springwebflux.handler.PlayerHandler;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.web.reactive.function.server.RouterFunction;
8+
import org.springframework.web.reactive.function.server.RouterFunctions;
9+
import org.springframework.web.reactive.function.server.ServerResponse;
10+
11+
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
12+
13+
@Configuration
14+
public class PlayerRouter {
15+
16+
@Bean
17+
public RouterFunction<ServerResponse> route(PlayerHandler playerHandler) {
18+
return RouterFunctions
19+
.route(GET("/players/{name}"), playerHandler::getName)
20+
.filter(new ExampleHandlerFilterFunction()::filter);
21+
}
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
logging.level.org.springframework.web=DEBUG
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.springwebflux.controller;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.test.context.junit4.SpringRunner;
8+
import org.springframework.test.web.reactive.server.EntityExchangeResult;
9+
import org.springframework.test.web.reactive.server.WebTestClient;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
@RunWith(SpringRunner.class)
14+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
15+
public class UserControllerTest {
16+
17+
@Autowired
18+
private WebTestClient webTestClient;
19+
20+
@Test
21+
public void getUserName() {
22+
EntityExchangeResult<String> result = webTestClient.get().uri("/users/fabio")
23+
.exchange()
24+
.expectStatus().isOk()
25+
.expectBody(String.class)
26+
.returnResult();
27+
28+
assertEquals(result.getResponseBody(), "fabio");
29+
assertEquals(result.getResponseHeaders().getFirst("web-filter"), "web-filter-test");
30+
}
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.springwebflux.handler;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.test.context.junit4.SpringRunner;
8+
import org.springframework.test.web.reactive.server.EntityExchangeResult;
9+
import org.springframework.test.web.reactive.server.WebTestClient;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
@RunWith(SpringRunner.class)
14+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
15+
public class PlayerHandlerTest {
16+
17+
@Autowired
18+
private WebTestClient webTestClient;
19+
20+
@Test
21+
public void getPlayerName() {
22+
EntityExchangeResult<String> result = webTestClient.get().uri("/players/fabio")
23+
.exchange()
24+
.expectStatus().isOk()
25+
.expectBody(String.class)
26+
.returnResult();
27+
28+
assertEquals(result.getResponseBody(), "fabio");
29+
assertEquals(result.getResponseHeaders().getFirst("web-filter"), "web-filter-test");
30+
}
31+
32+
@Test
33+
public void getForbiddenIfPlayerNameIsTest() {
34+
webTestClient.get().uri("/players/test")
35+
.exchange()
36+
.expectStatus().isForbidden();
37+
}
38+
39+
}

0 commit comments

Comments
 (0)