Skip to content

Commit 59f6044

Browse files
committed
Add route(RequestPredicate, HandlerFunction) to RouterFunctions builder
Closes gh-22701
1 parent 49570ae commit 59f6044

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctionBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -140,6 +140,12 @@ public RouterFunctions.Builder OPTIONS(String pattern, RequestPredicate predicat
140140
return add(RequestPredicates.OPTIONS(pattern).and(predicate), handlerFunction);
141141
}
142142

143+
@Override
144+
public RouterFunctions.Builder route(RequestPredicate predicate,
145+
HandlerFunction<ServerResponse> handlerFunction) {
146+
return add(RouterFunctions.route(predicate, handlerFunction));
147+
}
148+
143149
@Override
144150
public RouterFunctions.Builder resources(String pattern, Resource location) {
145151
return add(RouterFunctions.resources(pattern, location));

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,18 @@ public interface Builder {
477477
*/
478478
Builder OPTIONS(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction);
479479

480+
/**
481+
* Adds a route to the given handler function that handles all requests that match the
482+
* given predicate.
483+
*
484+
* @param predicate the request predicate to match
485+
* @param handlerFunction the handler function to handle all requests that match the predicate
486+
* @return this builder
487+
* @since 5.2
488+
* @see RequestPredicates
489+
*/
490+
Builder route(RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction);
491+
480492
/**
481493
* Adds the given route to this builder. Can be used to merge externally defined router
482494
* functions into this builder, or can be combined with

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionBuilderTests.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.springframework.http.MediaType;
3131

3232
import static org.junit.Assert.*;
33+
import static org.springframework.web.reactive.function.server.RequestPredicates.HEAD;
3334

3435
/**
3536
* @author Arjen Poutsma
@@ -41,23 +42,37 @@ public void route() {
4142
RouterFunction<ServerResponse> route = RouterFunctions.route()
4243
.GET("/foo", request -> ServerResponse.ok().build())
4344
.POST("/", RequestPredicates.contentType(MediaType.TEXT_PLAIN), request -> ServerResponse.noContent().build())
45+
.route(HEAD("/foo"), request -> ServerResponse.accepted().build())
4446
.build();
45-
System.out.println(route);
4647

47-
MockServerRequest fooRequest = MockServerRequest.builder().
48+
MockServerRequest getFooRequest = MockServerRequest.builder().
4849
method(HttpMethod.GET).
4950
uri(URI.create("http://localhost/foo"))
5051
.build();
5152

52-
Mono<Integer> responseMono = route.route(fooRequest)
53-
.flatMap(handlerFunction -> handlerFunction.handle(fooRequest))
53+
Mono<Integer> responseMono = route.route(getFooRequest)
54+
.flatMap(handlerFunction -> handlerFunction.handle(getFooRequest))
5455
.map(ServerResponse::statusCode)
5556
.map(HttpStatus::value);
5657

5758
StepVerifier.create(responseMono)
5859
.expectNext(200)
5960
.verifyComplete();
6061

62+
MockServerRequest headFooRequest = MockServerRequest.builder().
63+
method(HttpMethod.HEAD).
64+
uri(URI.create("http://localhost/foo"))
65+
.build();
66+
67+
responseMono = route.route(headFooRequest)
68+
.flatMap(handlerFunction -> handlerFunction.handle(getFooRequest))
69+
.map(ServerResponse::statusCode)
70+
.map(HttpStatus::value);
71+
72+
StepVerifier.create(responseMono)
73+
.expectNext(202)
74+
.verifyComplete();
75+
6176
MockServerRequest barRequest = MockServerRequest.builder().
6277
method(HttpMethod.POST).
6378
uri(URI.create("http://localhost/"))

0 commit comments

Comments
 (0)