Add route(RequestPredicate, HandlerFunction) to RouterFunctions builder
Closes gh-22701
This commit is contained in:
parent
49570ae2f3
commit
59f6044c09
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -140,6 +140,12 @@ class RouterFunctionBuilder implements RouterFunctions.Builder {
|
|||
return add(RequestPredicates.OPTIONS(pattern).and(predicate), handlerFunction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RouterFunctions.Builder route(RequestPredicate predicate,
|
||||
HandlerFunction<ServerResponse> handlerFunction) {
|
||||
return add(RouterFunctions.route(predicate, handlerFunction));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RouterFunctions.Builder resources(String pattern, Resource location) {
|
||||
return add(RouterFunctions.resources(pattern, location));
|
||||
|
|
|
@ -477,6 +477,18 @@ public abstract class RouterFunctions {
|
|||
*/
|
||||
Builder OPTIONS(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction);
|
||||
|
||||
/**
|
||||
* Adds a route to the given handler function that handles all requests that match the
|
||||
* given predicate.
|
||||
*
|
||||
* @param predicate the request predicate to match
|
||||
* @param handlerFunction the handler function to handle all requests that match the predicate
|
||||
* @return this builder
|
||||
* @since 5.2
|
||||
* @see RequestPredicates
|
||||
*/
|
||||
Builder route(RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction);
|
||||
|
||||
/**
|
||||
* Adds the given route to this builder. Can be used to merge externally defined router
|
||||
* functions into this builder, or can be combined with
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -30,6 +30,7 @@ import org.springframework.http.HttpStatus;
|
|||
import org.springframework.http.MediaType;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.HEAD;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
|
@ -41,16 +42,16 @@ public class RouterFunctionBuilderTests {
|
|||
RouterFunction<ServerResponse> route = RouterFunctions.route()
|
||||
.GET("/foo", request -> ServerResponse.ok().build())
|
||||
.POST("/", RequestPredicates.contentType(MediaType.TEXT_PLAIN), request -> ServerResponse.noContent().build())
|
||||
.route(HEAD("/foo"), request -> ServerResponse.accepted().build())
|
||||
.build();
|
||||
System.out.println(route);
|
||||
|
||||
MockServerRequest fooRequest = MockServerRequest.builder().
|
||||
MockServerRequest getFooRequest = MockServerRequest.builder().
|
||||
method(HttpMethod.GET).
|
||||
uri(URI.create("http://localhost/foo"))
|
||||
.build();
|
||||
|
||||
Mono<Integer> responseMono = route.route(fooRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(fooRequest))
|
||||
Mono<Integer> responseMono = route.route(getFooRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(getFooRequest))
|
||||
.map(ServerResponse::statusCode)
|
||||
.map(HttpStatus::value);
|
||||
|
||||
|
@ -58,6 +59,20 @@ public class RouterFunctionBuilderTests {
|
|||
.expectNext(200)
|
||||
.verifyComplete();
|
||||
|
||||
MockServerRequest headFooRequest = MockServerRequest.builder().
|
||||
method(HttpMethod.HEAD).
|
||||
uri(URI.create("http://localhost/foo"))
|
||||
.build();
|
||||
|
||||
responseMono = route.route(headFooRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(getFooRequest))
|
||||
.map(ServerResponse::statusCode)
|
||||
.map(HttpStatus::value);
|
||||
|
||||
StepVerifier.create(responseMono)
|
||||
.expectNext(202)
|
||||
.verifyComplete();
|
||||
|
||||
MockServerRequest barRequest = MockServerRequest.builder().
|
||||
method(HttpMethod.POST).
|
||||
uri(URI.create("http://localhost/"))
|
||||
|
|
Loading…
Reference in New Issue