Merge pull request #4045 from its-fpereira/BAEL-1505
BAEL-1505: Spring WebFlux Filters Refatoring
This commit is contained in:
commit
13a1ddcfc2
|
@ -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();
|
||||||
}
|
}
|
|
@ -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;
|
|
@ -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);
|
||||||
}
|
}
|
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
Loading…
Reference in New Issue