Switch to functional web code to use static imports

Update the samples and tests to use the more idiomatic static import
style.
This commit is contained in:
Phillip Webb 2018-06-04 17:27:34 -07:00
parent 8eba37500c
commit 571c50e43f
7 changed files with 29 additions and 31 deletions

View File

@ -45,9 +45,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction; 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 org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.restdocs.payload.PayloadDocumentation.beneathPath; import static org.springframework.restdocs.payload.PayloadDocumentation.beneathPath;
@ -55,6 +53,8 @@ import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWit
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document; import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document;
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.documentationConfiguration; import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.documentationConfiguration;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
/** /**
* Tests for generating documentation describing {@link MappingsEndpoint}. * Tests for generating documentation describing {@link MappingsEndpoint}.
@ -191,8 +191,7 @@ public class MappingsEndpointReactiveDocumentationTests
@Bean @Bean
public RouterFunction<ServerResponse> exampleRouter() { public RouterFunction<ServerResponse> exampleRouter() {
return RouterFunctions.route(RequestPredicates.GET("/foo"), return route(GET("/foo"), (request) -> ServerResponse.ok().build());
(request) -> ServerResponse.ok().build());
} }
@Bean @Bean

View File

@ -36,13 +36,13 @@ import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.config.EnableWebFlux; import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.function.server.HandlerFunction; import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction; 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 org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder; import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
/** /**
* Integration tests for {@link HttpTraceWebFilter}. * Integration tests for {@link HttpTraceWebFilter}.
@ -115,10 +115,9 @@ public class HttpTraceWebFilterIntegrationTests {
@Bean @Bean
public RouterFunction<ServerResponse> router() { public RouterFunction<ServerResponse> router() {
return RouterFunctions return route(GET("/mono-error"),
.route(RequestPredicates.GET("/mono-error"), (request) -> Mono.error(new RuntimeException())).andRoute(
(request) -> Mono.error(new RuntimeException())) GET("/thrown"),
.andRoute(RequestPredicates.GET("/thrown"),
(HandlerFunction<ServerResponse>) (request) -> { (HandlerFunction<ServerResponse>) (request) -> {
throw new RuntimeException(); throw new RuntimeException();
}); });

View File

@ -51,9 +51,7 @@ import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.reactive.config.EnableWebFlux; import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction; 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 org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@ -61,6 +59,9 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
/** /**
* Tests for {@link MappingsEndpoint}. * Tests for {@link MappingsEndpoint}.
@ -166,11 +167,8 @@ public class MappingsEndpointTests {
@Bean @Bean
public RouterFunction<ServerResponse> routerFunction() { public RouterFunction<ServerResponse> routerFunction() {
return RouterFunctions return route(GET("/one"), (request) -> ServerResponse.ok().build())
.route(RequestPredicates.GET("/one"), .andRoute(POST("/two"), (request) -> ServerResponse.ok().build());
(request) -> ServerResponse.ok().build())
.andRoute(RequestPredicates.POST("/two"),
(request) -> ServerResponse.ok().build());
} }
@RequestMapping("/three") @RequestMapping("/three")

View File

@ -35,13 +35,14 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RequestPredicate; import org.springframework.web.reactive.function.server.RequestPredicate;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
import static org.springframework.web.reactive.function.server.RequestPredicates.all;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
/** /**
* Basic global {@link org.springframework.web.server.WebExceptionHandler}, rendering * Basic global {@link org.springframework.web.server.WebExceptionHandler}, rendering
* {@link ErrorAttributes}. * {@link ErrorAttributes}.
@ -106,8 +107,8 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa
@Override @Override
protected RouterFunction<ServerResponse> getRoutingFunction( protected RouterFunction<ServerResponse> getRoutingFunction(
ErrorAttributes errorAttributes) { ErrorAttributes errorAttributes) {
return RouterFunctions.route(acceptsTextHtml(), this::renderErrorView) return route(acceptsTextHtml(), this::renderErrorView).andRoute(all(),
.andRoute(RequestPredicates.all(), this::renderErrorResponse); this::renderErrorResponse);
} }
/** /**

View File

@ -23,12 +23,12 @@ import org.springframework.boot.test.context.runner.ReactiveWebApplicationContex
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.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction; 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 org.springframework.web.reactive.function.server.ServerResponse;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
/** /**
* Tests for {@link HttpHandlerAutoConfiguration}. * Tests for {@link HttpHandlerAutoConfiguration}.
@ -69,8 +69,7 @@ public class HttpHandlerAutoConfigurationTests {
@Bean @Bean
public RouterFunction<ServerResponse> routerFunction() { public RouterFunction<ServerResponse> routerFunction() {
return RouterFunctions.route(RequestPredicates.GET("/test"), return route(GET("/test"), (serverRequest) -> null);
(serverRequest) -> null);
} }
} }

View File

@ -19,11 +19,12 @@ package sample.secure.webflux;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction; 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 org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
@SpringBootApplication @SpringBootApplication
public class SampleSecureWebFluxApplication { public class SampleSecureWebFluxApplication {
@ -33,7 +34,7 @@ public class SampleSecureWebFluxApplication {
@Bean @Bean
public RouterFunction<ServerResponse> monoRouterFunction(EchoHandler echoHandler) { public RouterFunction<ServerResponse> monoRouterFunction(EchoHandler echoHandler) {
return RouterFunctions.route(RequestPredicates.POST("/echo"), echoHandler::echo); return route(POST("/echo"), echoHandler::echo);
} }
} }

View File

@ -19,11 +19,12 @@ package sample.webflux;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction; 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 org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
@SpringBootApplication @SpringBootApplication
public class SampleWebFluxApplication { public class SampleWebFluxApplication {
@ -33,7 +34,7 @@ public class SampleWebFluxApplication {
@Bean @Bean
public RouterFunction<ServerResponse> monoRouterFunction(EchoHandler echoHandler) { public RouterFunction<ServerResponse> monoRouterFunction(EchoHandler echoHandler) {
return RouterFunctions.route(RequestPredicates.POST("/echo"), echoHandler::echo); return route(POST("/echo"), echoHandler::echo);
} }
} }