Skip to content

Commit

Permalink
Switch to functional web code to use static imports
Browse files Browse the repository at this point in the history
Update the samples and tests to use the more idiomatic static import
style.
  • Loading branch information
philwebb committed Jun 5, 2018
1 parent 8eba375 commit 571c50e
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 31 deletions.
Expand Up @@ -45,16 +45,16 @@
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.bind.annotation.PostMapping;
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.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.restdocs.payload.PayloadDocumentation.beneathPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document;
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}.
Expand Down Expand Up @@ -191,8 +191,7 @@ public MappingsEndpoint mappingsEndpoint(

@Bean
public RouterFunction<ServerResponse> exampleRouter() {
return RouterFunctions.route(RequestPredicates.GET("/foo"),
(request) -> ServerResponse.ok().build());
return route(GET("/foo"), (request) -> ServerResponse.ok().build());
}

@Bean
Expand Down
Expand Up @@ -36,13 +36,13 @@
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.config.EnableWebFlux;
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.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;

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}.
Expand Down Expand Up @@ -115,10 +115,9 @@ public HttpHandler httpHandler(ApplicationContext applicationContext) {

@Bean
public RouterFunction<ServerResponse> router() {
return RouterFunctions
.route(RequestPredicates.GET("/mono-error"),
(request) -> Mono.error(new RuntimeException()))
.andRoute(RequestPredicates.GET("/thrown"),
return route(GET("/mono-error"),
(request) -> Mono.error(new RuntimeException())).andRoute(
GET("/thrown"),
(HandlerFunction<ServerResponse>) (request) -> {
throw new RuntimeException();
});
Expand Down
Expand Up @@ -51,16 +51,17 @@
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
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.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
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}.
Expand Down Expand Up @@ -166,11 +167,8 @@ public DispatcherHandlersMappingDescriptionProvider dispatcherHandlersMappingDes

@Bean
public RouterFunction<ServerResponse> routerFunction() {
return RouterFunctions
.route(RequestPredicates.GET("/one"),
(request) -> ServerResponse.ok().build())
.andRoute(RequestPredicates.POST("/two"),
(request) -> ServerResponse.ok().build());
return route(GET("/one"), (request) -> ServerResponse.ok().build())
.andRoute(POST("/two"), (request) -> ServerResponse.ok().build());
}

@RequestMapping("/three")
Expand Down
Expand Up @@ -35,13 +35,14 @@
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
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.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
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
* {@link ErrorAttributes}.
Expand Down Expand Up @@ -106,8 +107,8 @@ public DefaultErrorWebExceptionHandler(ErrorAttributes errorAttributes,
@Override
protected RouterFunction<ServerResponse> getRoutingFunction(
ErrorAttributes errorAttributes) {
return RouterFunctions.route(acceptsTextHtml(), this::renderErrorView)
.andRoute(RequestPredicates.all(), this::renderErrorResponse);
return route(acceptsTextHtml(), this::renderErrorView).andRoute(all(),
this::renderErrorResponse);
}

/**
Expand Down
Expand Up @@ -23,12 +23,12 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

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}.
Expand Down Expand Up @@ -69,8 +69,7 @@ public HttpHandler customHttpHandler() {

@Bean
public RouterFunction<ServerResponse> routerFunction() {
return RouterFunctions.route(RequestPredicates.GET("/test"),
(serverRequest) -> null);
return route(GET("/test"), (serverRequest) -> null);
}

}
Expand Down
Expand Up @@ -19,11 +19,12 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.RouterFunctions;
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
public class SampleSecureWebFluxApplication {

Expand All @@ -33,7 +34,7 @@ public static void main(String[] args) {

@Bean
public RouterFunction<ServerResponse> monoRouterFunction(EchoHandler echoHandler) {
return RouterFunctions.route(RequestPredicates.POST("/echo"), echoHandler::echo);
return route(POST("/echo"), echoHandler::echo);
}

}
Expand Up @@ -19,11 +19,12 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.RouterFunctions;
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
public class SampleWebFluxApplication {

Expand All @@ -33,7 +34,7 @@ public static void main(String[] args) {

@Bean
public RouterFunction<ServerResponse> monoRouterFunction(EchoHandler echoHandler) {
return RouterFunctions.route(RequestPredicates.POST("/echo"), echoHandler::echo);
return route(POST("/echo"), echoHandler::echo);
}

}

0 comments on commit 571c50e

Please sign in to comment.