Merge pull request #4045 from its-fpereira/BAEL-1505

BAEL-1505: Spring WebFlux Filters Refatoring
This commit is contained in:
Tom Hombergs 2018-04-21 22:28:35 +02:00 committed by GitHub
commit 13a1ddcfc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 12 additions and 14 deletions

View File

@ -1,4 +1,4 @@
package com.baeldung.reactive.filter; package com.baeldung.reactive.filters;
import org.springframework.web.reactive.function.server.HandlerFilterFunction; import org.springframework.web.reactive.function.server.HandlerFilterFunction;
import org.springframework.web.reactive.function.server.HandlerFunction; import org.springframework.web.reactive.function.server.HandlerFunction;
@ -8,10 +8,10 @@ import reactor.core.publisher.Mono;
import static org.springframework.http.HttpStatus.FORBIDDEN; import static org.springframework.http.HttpStatus.FORBIDDEN;
public class ExampleHandlerFilterFunction implements HandlerFilterFunction { public class ExampleHandlerFilterFunction implements HandlerFilterFunction<ServerResponse, ServerResponse> {
@Override @Override
public Mono filter(ServerRequest serverRequest, HandlerFunction handlerFunction) { public Mono<ServerResponse> filter(ServerRequest serverRequest, HandlerFunction<ServerResponse> handlerFunction) {
if (serverRequest.pathVariable("name").equalsIgnoreCase("test")) { if (serverRequest.pathVariable("name").equalsIgnoreCase("test")) {
return ServerResponse.status(FORBIDDEN).build(); return ServerResponse.status(FORBIDDEN).build();
} }

View File

@ -1,4 +1,4 @@
package com.baeldung.reactive.filter; package com.baeldung.reactive.filters;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;

View File

@ -1,4 +1,4 @@
package com.baeldung.reactive.handler; package com.baeldung.reactive.filters;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerRequest;
@ -8,9 +8,9 @@ import reactor.core.publisher.Mono;
import static org.springframework.web.reactive.function.server.ServerResponse.ok; import static org.springframework.web.reactive.function.server.ServerResponse.ok;
@Component @Component
public class PlayerHandler { class PlayerHandler {
public Mono<ServerResponse> getName(ServerRequest request) { Mono<ServerResponse> getName(ServerRequest request) {
Mono<String> name = Mono.just(request.pathVariable("name")); Mono<String> name = Mono.just(request.pathVariable("name"));
return ok().body(name, String.class); return ok().body(name, String.class);
} }

View File

@ -1,7 +1,5 @@
package com.baeldung.reactive.router; package com.baeldung.reactive.filters;
import com.baeldung.reactive.filter.ExampleHandlerFilterFunction;
import com.baeldung.reactive.handler.PlayerHandler;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunction;
@ -17,6 +15,6 @@ public class PlayerRouter {
public RouterFunction<ServerResponse> route(PlayerHandler playerHandler) { public RouterFunction<ServerResponse> route(PlayerHandler playerHandler) {
return RouterFunctions return RouterFunctions
.route(GET("/players/{name}"), playerHandler::getName) .route(GET("/players/{name}"), playerHandler::getName)
.filter(new ExampleHandlerFilterFunction()::filter); .filter(new ExampleHandlerFilterFunction());
} }
} }

View File

@ -1,4 +1,4 @@
package com.baeldung.reactive.controller; package com.baeldung.reactive.filters;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;

View File

@ -1,4 +1,4 @@
package com.baeldung.reactive.handler; package com.baeldung.reactive.filters;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;

View File

@ -1,4 +1,4 @@
package com.baeldung.reactive.controller; package com.baeldung.reactive.filters;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;