From 9f21415451acfbd37632ec41d423a9acbb116efd Mon Sep 17 00:00:00 2001 From: Fabio Pereira Date: Thu, 22 Mar 2018 01:25:15 -0300 Subject: [PATCH] add: functional endpoint and annotated controller --- spring-webflux/pom.xml | 86 ++++++++----------- .../controller/UserController.java | 15 ++++ .../filter/ExampleHandlerFilterFunction.java | 22 +++++ .../filter/ExampleWebFilter.java | 17 ++++ .../springwebflux/handler/PlayerHandler.java | 17 ++++ .../springwebflux/router/PlayerRouter.java | 22 +++++ .../src/main/resources/application.properties | 1 + .../controller/UserControllerTest.java | 31 +++++++ .../handler/PlayerHandlerTest.java | 39 +++++++++ 9 files changed, 202 insertions(+), 48 deletions(-) create mode 100644 spring-webflux/src/main/java/com/baeldung/springwebflux/controller/UserController.java create mode 100644 spring-webflux/src/main/java/com/baeldung/springwebflux/filter/ExampleHandlerFilterFunction.java create mode 100644 spring-webflux/src/main/java/com/baeldung/springwebflux/filter/ExampleWebFilter.java create mode 100644 spring-webflux/src/main/java/com/baeldung/springwebflux/handler/PlayerHandler.java create mode 100644 spring-webflux/src/main/java/com/baeldung/springwebflux/router/PlayerRouter.java create mode 100644 spring-webflux/src/test/java/com/baeldung/springwebflux/controller/UserControllerTest.java create mode 100644 spring-webflux/src/test/java/com/baeldung/springwebflux/handler/PlayerHandlerTest.java diff --git a/spring-webflux/pom.xml b/spring-webflux/pom.xml index 621129c4a4..f652c2d27b 100644 --- a/spring-webflux/pom.xml +++ b/spring-webflux/pom.xml @@ -1,60 +1,50 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.baeldung - spring-webflux - 0.0.1-SNAPSHOT - jar + com.baeldung + spring-webflux + 0.0.1-SNAPSHOT + jar - spring-webflux - Spring Webflux sample project about filters + spring-webflux + Spring Webflux sample project about filters - - org.springframework.boot - spring-boot-starter-parent - 2.0.0.RELEASE - - + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.RELEASE + + - - UTF-8 - UTF-8 - 1.8 - + + UTF-8 + UTF-8 + 1.8 + - - - org.springframework.boot - spring-boot-starter-webflux - + + + org.springframework.boot + spring-boot-starter-webflux + - - org.springframework.boot - spring-boot-devtools - runtime - - - org.springframework.boot - spring-boot-starter-test - test - - - io.projectreactor - reactor-test - test - - + + org.springframework.boot + spring-boot-starter-test + test + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/spring-webflux/src/main/java/com/baeldung/springwebflux/controller/UserController.java b/spring-webflux/src/main/java/com/baeldung/springwebflux/controller/UserController.java new file mode 100644 index 0000000000..cd40b1c81d --- /dev/null +++ b/spring-webflux/src/main/java/com/baeldung/springwebflux/controller/UserController.java @@ -0,0 +1,15 @@ +package com.baeldung.springwebflux.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Mono; + +@RestController +public class UserController { + + @GetMapping(path = "/users/{name}") + public Mono getName(@PathVariable String name) { + return Mono.just(name); + } +} diff --git a/spring-webflux/src/main/java/com/baeldung/springwebflux/filter/ExampleHandlerFilterFunction.java b/spring-webflux/src/main/java/com/baeldung/springwebflux/filter/ExampleHandlerFilterFunction.java new file mode 100644 index 0000000000..c1a8069a2b --- /dev/null +++ b/spring-webflux/src/main/java/com/baeldung/springwebflux/filter/ExampleHandlerFilterFunction.java @@ -0,0 +1,22 @@ +package com.baeldung.springwebflux.filter; + +import org.springframework.web.reactive.function.server.HandlerFilterFunction; +import org.springframework.web.reactive.function.server.HandlerFunction; +import org.springframework.web.reactive.function.server.ServerRequest; +import org.springframework.web.reactive.function.server.ServerResponse; +import reactor.core.publisher.Mono; + +import static org.springframework.http.HttpStatus.FORBIDDEN; + +public class ExampleHandlerFilterFunction implements HandlerFilterFunction { + + @Override + public Mono filter(ServerRequest serverRequest, HandlerFunction handlerFunction) { + if (serverRequest.pathVariable("name").equalsIgnoreCase("test")) { + return ServerResponse.status(FORBIDDEN).build(); + } + return handlerFunction.handle(serverRequest); + } + +} + diff --git a/spring-webflux/src/main/java/com/baeldung/springwebflux/filter/ExampleWebFilter.java b/spring-webflux/src/main/java/com/baeldung/springwebflux/filter/ExampleWebFilter.java new file mode 100644 index 0000000000..8dad358744 --- /dev/null +++ b/spring-webflux/src/main/java/com/baeldung/springwebflux/filter/ExampleWebFilter.java @@ -0,0 +1,17 @@ +package com.baeldung.springwebflux.filter; + +import org.springframework.stereotype.Component; +import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.server.WebFilter; +import org.springframework.web.server.WebFilterChain; +import reactor.core.publisher.Mono; + +@Component +public class ExampleWebFilter implements WebFilter { + + @Override + public Mono filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) { + serverWebExchange.getResponse().getHeaders().add("web-filter", "web-filter-test"); + return webFilterChain.filter(serverWebExchange); + } +} diff --git a/spring-webflux/src/main/java/com/baeldung/springwebflux/handler/PlayerHandler.java b/spring-webflux/src/main/java/com/baeldung/springwebflux/handler/PlayerHandler.java new file mode 100644 index 0000000000..c5e8ae1bd7 --- /dev/null +++ b/spring-webflux/src/main/java/com/baeldung/springwebflux/handler/PlayerHandler.java @@ -0,0 +1,17 @@ +package com.baeldung.springwebflux.handler; + +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.server.ServerRequest; +import org.springframework.web.reactive.function.server.ServerResponse; +import reactor.core.publisher.Mono; + +import static org.springframework.web.reactive.function.server.ServerResponse.ok; + +@Component +public class PlayerHandler { + + public Mono getName(ServerRequest request) { + Mono name = Mono.just(request.pathVariable("name")); + return ok().body(name, String.class); + } +} diff --git a/spring-webflux/src/main/java/com/baeldung/springwebflux/router/PlayerRouter.java b/spring-webflux/src/main/java/com/baeldung/springwebflux/router/PlayerRouter.java new file mode 100644 index 0000000000..5836906be8 --- /dev/null +++ b/spring-webflux/src/main/java/com/baeldung/springwebflux/router/PlayerRouter.java @@ -0,0 +1,22 @@ +package com.baeldung.springwebflux.router; + +import com.baeldung.springwebflux.filter.ExampleHandlerFilterFunction; +import com.baeldung.springwebflux.handler.PlayerHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +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 static org.springframework.web.reactive.function.server.RequestPredicates.GET; + +@Configuration +public class PlayerRouter { + + @Bean + public RouterFunction route(PlayerHandler playerHandler) { + return RouterFunctions + .route(GET("/players/{name}"), playerHandler::getName) + .filter(new ExampleHandlerFilterFunction()::filter); + } +} diff --git a/spring-webflux/src/main/resources/application.properties b/spring-webflux/src/main/resources/application.properties index e69de29bb2..7ecf7b5ec5 100644 --- a/spring-webflux/src/main/resources/application.properties +++ b/spring-webflux/src/main/resources/application.properties @@ -0,0 +1 @@ +logging.level.org.springframework.web=DEBUG \ No newline at end of file diff --git a/spring-webflux/src/test/java/com/baeldung/springwebflux/controller/UserControllerTest.java b/spring-webflux/src/test/java/com/baeldung/springwebflux/controller/UserControllerTest.java new file mode 100644 index 0000000000..e8d8a5ec7f --- /dev/null +++ b/spring-webflux/src/test/java/com/baeldung/springwebflux/controller/UserControllerTest.java @@ -0,0 +1,31 @@ +package com.baeldung.springwebflux.controller; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.reactive.server.EntityExchangeResult; +import org.springframework.test.web.reactive.server.WebTestClient; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class UserControllerTest { + + @Autowired + private WebTestClient webTestClient; + + @Test + public void getUserName() { + EntityExchangeResult result = webTestClient.get().uri("/users/fabio") + .exchange() + .expectStatus().isOk() + .expectBody(String.class) + .returnResult(); + + assertEquals(result.getResponseBody(), "fabio"); + assertEquals(result.getResponseHeaders().getFirst("web-filter"), "web-filter-test"); + } +} \ No newline at end of file diff --git a/spring-webflux/src/test/java/com/baeldung/springwebflux/handler/PlayerHandlerTest.java b/spring-webflux/src/test/java/com/baeldung/springwebflux/handler/PlayerHandlerTest.java new file mode 100644 index 0000000000..3a7d4d0493 --- /dev/null +++ b/spring-webflux/src/test/java/com/baeldung/springwebflux/handler/PlayerHandlerTest.java @@ -0,0 +1,39 @@ +package com.baeldung.springwebflux.handler; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.reactive.server.EntityExchangeResult; +import org.springframework.test.web.reactive.server.WebTestClient; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class PlayerHandlerTest { + + @Autowired + private WebTestClient webTestClient; + + @Test + public void getPlayerName() { + EntityExchangeResult result = webTestClient.get().uri("/players/fabio") + .exchange() + .expectStatus().isOk() + .expectBody(String.class) + .returnResult(); + + assertEquals(result.getResponseBody(), "fabio"); + assertEquals(result.getResponseHeaders().getFirst("web-filter"), "web-filter-test"); + } + + @Test + public void getForbiddenIfPlayerNameIsTest() { + webTestClient.get().uri("/players/test") + .exchange() + .expectStatus().isForbidden(); + } + +} \ No newline at end of file