Introduce RouterFunction attributes

This commit introduces support for router function attributes, a
way to associate meta-data with a route.

Closes: gh-25938
This commit is contained in:
Arjen Poutsma 2020-10-15 15:52:58 +02:00
parent 8d86d61f9f
commit 417e7e03d4
17 changed files with 712 additions and 0 deletions

View File

@ -16,6 +16,7 @@
package org.springframework.web.reactive.function.server;
import java.util.Map;
import java.util.function.Function;
import reactor.core.publisher.Mono;
@ -60,6 +61,10 @@ class ChangePathPatternParserVisitor implements RouterFunctions.Visitor {
public void resources(Function<ServerRequest, Mono<Resource>> lookupFunction) {
}
@Override
public void attributes(Map<String, Object> attributes) {
}
@Override
public void unknown(RouterFunction<?> routerFunction) {
}

View File

@ -16,8 +16,14 @@
package org.springframework.web.reactive.function.server;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;
import reactor.core.publisher.Mono;
import org.springframework.util.Assert;
/**
* Represents a function that routes to a {@linkplain HandlerFunction handler function}.
*
@ -115,4 +121,39 @@ public interface RouterFunction<T extends ServerResponse> {
visitor.unknown(this);
}
/**
* Return a new routing function with the given attribute.
* @param name the attribute name
* @param value the attribute value
* @return a function that has the specified attributes
* @since 5.3
*/
default RouterFunction<T> withAttribute(String name, Object value) {
Assert.hasLength(name, "Name must not be empty");
Assert.notNull(value, "Value must not be null");
Map<String, Object> attributes = new LinkedHashMap<>();
attributes.put(name, value);
return new RouterFunctions.AttributesRouterFunction<>(this, attributes);
}
/**
* Return a new routing function with attributes manipulated with the given consumer.
* <p>The map provided to the consumer is "live", so that the consumer can be used
* to {@linkplain Map#put(Object, Object) overwrite} existing attributes,
* {@linkplain Map#remove(Object) remove} attributes, or use any of the other
* {@link Map} methods.
* @param attributesConsumer a function that consumes the attributes map
* @return this builder
* @since 5.3
*/
default RouterFunction<T> withAttributes(Consumer<Map<String, Object>> attributesConsumer) {
Assert.notNull(attributesConsumer, "AttributesConsumer must not be null");
Map<String, Object> attributes = new LinkedHashMap<>();
attributesConsumer.accept(attributes);
return new RouterFunctions.AttributesRouterFunction<>(this, attributes);
}
}

View File

@ -18,6 +18,7 @@ package org.springframework.web.reactive.function.server;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
@ -330,6 +331,35 @@ class RouterFunctionBuilder implements RouterFunctions.Builder {
return this;
}
@Override
public RouterFunctions.Builder withAttribute(String name, Object value) {
Assert.hasLength(name, "Name must not be empty");
Assert.notNull(value, "Value must not be null");
if (this.routerFunctions.isEmpty()) {
throw new IllegalStateException("attributes can only be called after any other method (GET, path, etc.)");
}
int lastIdx = this.routerFunctions.size() - 1;
RouterFunction<ServerResponse> attributed = this.routerFunctions.get(lastIdx)
.withAttribute(name, value);
this.routerFunctions.set(lastIdx, attributed);
return this;
}
@Override
public RouterFunctions.Builder withAttributes(Consumer<Map<String, Object>> attributesConsumer) {
Assert.notNull(attributesConsumer, "AttributesConsumer must not be null");
if (this.routerFunctions.isEmpty()) {
throw new IllegalStateException("attributes can only be called after any other method (GET, path, etc.)");
}
int lastIdx = this.routerFunctions.size() - 1;
RouterFunction<ServerResponse> attributed = this.routerFunctions.get(lastIdx)
.withAttributes(attributesConsumer);
this.routerFunctions.set(lastIdx, attributed);
return this;
}
@Override
public RouterFunction<ServerResponse> build() {
if (this.routerFunctions.isEmpty()) {

View File

@ -16,6 +16,8 @@
package org.springframework.web.reactive.function.server;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
@ -858,6 +860,27 @@ public abstract class RouterFunctions {
<T extends Throwable> Builder onError(Class<T> exceptionType,
BiFunction<? super T, ServerRequest, Mono<ServerResponse>> responseProvider);
/**
* Add an attribute with the given name and value to the last route built with this builder.
* @param name the attribute name
* @param value the attribute value
* @return this builder
* @since 5.3
*/
Builder withAttribute(String name, Object value);
/**
* Manipulate the attributes of the last route built with the given consumer.
* <p>The map provided to the consumer is "live", so that the consumer can be used
* to {@linkplain Map#put(Object, Object) overwrite} existing attributes,
* {@linkplain Map#remove(Object) remove} attributes, or use any of the other
* {@link Map} methods.
* @param attributesConsumer a function that consumes the attributes map
* @return this builder
* @since 5.3
*/
Builder withAttributes(Consumer<Map<String, Object>> attributesConsumer);
/**
* Builds the {@code RouterFunction}. All created routes are
* {@linkplain RouterFunction#and(RouterFunction) composed} with one another, and filters
@ -902,6 +925,14 @@ public abstract class RouterFunctions {
*/
void resources(Function<ServerRequest, Mono<Resource>> lookupFunction);
/**
* Receive notification of a router function with attributes. The
* given attributes apply to the router notification that follows this one.
* @param attributes the attributes that apply to the following router
* @since 5.3
*/
void attributes(Map<String, Object> attributes);
/**
* Receive notification of an unknown router function. This method is called for router
* functions that were not created via the various {@link RouterFunctions} methods.
@ -1126,6 +1157,58 @@ public abstract class RouterFunctions {
}
static final class AttributesRouterFunction<T extends ServerResponse> extends AbstractRouterFunction<T> {
private final RouterFunction<T> delegate;
private final Map<String,Object> attributes;
public AttributesRouterFunction(RouterFunction<T> delegate, Map<String, Object> attributes) {
this.delegate = delegate;
this.attributes = initAttributes(attributes);
}
private static Map<String, Object> initAttributes(Map<String, Object> attributes) {
if (attributes.isEmpty()) {
return Collections.emptyMap();
}
else {
return Collections.unmodifiableMap(new LinkedHashMap<>(attributes));
}
}
@Override
public Mono<HandlerFunction<T>> route(ServerRequest request) {
return this.delegate.route(request);
}
@Override
public void accept(Visitor visitor) {
visitor.attributes(this.attributes);
this.delegate.accept(visitor);
}
@Override
public RouterFunction<T> withAttribute(String name, Object value) {
Assert.hasLength(name, "Name must not be empty");
Assert.notNull(value, "Value must not be null");
Map<String, Object> attributes = new LinkedHashMap<>(this.attributes);
attributes.put(name, value);
return new AttributesRouterFunction<>(this.delegate, attributes);
}
@Override
public RouterFunction<T> withAttributes(Consumer<Map<String, Object>> attributesConsumer) {
Assert.notNull(attributesConsumer, "AttributesConsumer must not be null");
Map<String, Object> attributes = new LinkedHashMap<>(this.attributes);
attributesConsumer.accept(attributes);
return new AttributesRouterFunction<>(this.delegate, attributes);
}
}
private static class HandlerStrategiesResponseContext implements ServerResponse.Context {
private final HandlerStrategies strategies;

View File

@ -16,6 +16,7 @@
package org.springframework.web.reactive.function.server;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
@ -69,6 +70,10 @@ class ToStringVisitor implements RouterFunctions.Visitor, RequestPredicates.Visi
this.builder.append(lookupFunction).append('\n');
}
@Override
public void attributes(Map<String, Object> attributes) {
}
@Override
public void unknown(RouterFunction<?> routerFunction) {
indent();

View File

@ -0,0 +1,73 @@
/*
* Copyright 2002-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.function.server;
import java.util.Map;
import java.util.function.Function;
import reactor.core.publisher.Mono;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
/**
* @author Arjen Poutsma
*/
class AttributesTestVisitor implements RouterFunctions.Visitor {
@Nullable
private Map<String, Object> attributes;
private int visitCount;
public int visitCount() {
return this.visitCount;
}
@Override
public void startNested(RequestPredicate predicate) {
}
@Override
public void endNested(RequestPredicate predicate) {
}
@Override
public void route(RequestPredicate predicate, HandlerFunction<?> handlerFunction) {
assertThat(this.attributes).isNotNull();
this.attributes = null;
}
@Override
public void resources(Function<ServerRequest, Mono<Resource>> lookupFunction) {
}
@Override
public void attributes(Map<String, Object> attributes) {
assertThat(attributes).containsExactly(entry("foo", "bar"), entry("baz", "qux"));
this.attributes = attributes;
this.visitCount++;
}
@Override
public void unknown(RouterFunction<?> routerFunction) {
}
}

View File

@ -0,0 +1,157 @@
/*
* Copyright 2002-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.function.server;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import reactor.core.publisher.Mono;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
/**
* @author Arjen Poutsma
*/
public class CustomRouteBuilder {
public static final String OPERATION_ATTRIBUTE = CustomRouteBuilder.class.getName() + ".operation";
private final RouterFunctions.Builder delegate = RouterFunctions.route();
private CustomRouteBuilder() {
}
public static CustomRouteBuilder route() {
return new CustomRouteBuilder();
}
public CustomRouteBuilder GET(String pattern, HandlerFunction<ServerResponse> handlerFunction,
Consumer<OperationBuilder> operationsConsumer) {
OperationBuilder builder = new OperationBuilder();
operationsConsumer.accept(builder);
this.delegate.GET(pattern, handlerFunction)
.withAttribute(OPERATION_ATTRIBUTE, builder.operation);
return this;
}
public RouterFunction<ServerResponse> build() {
return this.delegate.build();
}
public static void main(String[] args) {
RouterFunction<ServerResponse> routerFunction =
route()
.GET("/foo", request -> ServerResponse.ok().build(), ops -> ops
.parameter("key1", "My key1 description")
.parameter("key1", "My key1 description")
.response(200, "This is normal response description")
.response(404, "This is response description")
)
.build();
AttributesVisitor visitor = new AttributesVisitor();
routerFunction.accept(visitor);
}
public static class OperationBuilder {
private final Operation operation = new Operation();
public OperationBuilder parameter(String name, String description) {
this.operation.parameter(name, description);
return this;
}
public OperationBuilder response(int statusCode, String description) {
this.operation.response(statusCode, description);
return this;
}
}
static class Operation {
private final Map<String, String> parameters = new LinkedHashMap<>();
private final Map<Integer, String > responses = new LinkedHashMap<>();
public void parameter(String name, String description) {
this.parameters.put(name, description);
}
public void response(int status, String description) {
this.responses.put(status, description);
}
@Override
public String toString() {
return "parameters=" + parameters +
", responses=" + responses;
}
}
static class AttributesVisitor implements RouterFunctions.Visitor {
@Nullable
private Map<String, Object> attributes;
@Override
public void attributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
@Override
public void route(RequestPredicate predicate, HandlerFunction<?> handlerFunction) {
System.out.printf("Route predicate %s->%s%nhas attributes %s", predicate, handlerFunction, this.attributes);
this.attributes = null;
}
@Override
public void startNested(RequestPredicate predicate) {
// TODO
}
@Override
public void endNested(RequestPredicate predicate) {
// TODO
}
@Override
public void resources(Function<ServerRequest, Mono<Resource>> lookupFunction) {
// TODO
}
@Override
public void unknown(RouterFunction<?> routerFunction) {
// TODO
}
}
}

View File

@ -232,5 +232,23 @@ public class RouterFunctionBuilderTests {
}
@Test
public void attributes() {
RouterFunction<ServerResponse> route = RouterFunctions.route()
.GET("/atts/1", request -> ServerResponse.ok().build())
.withAttribute("foo", "bar")
.withAttribute("baz", "qux")
.GET("/atts/2", request -> ServerResponse.ok().build())
.withAttributes(atts -> {
atts.put("foo", "bar");
atts.put("baz", "qux");
})
.build();
AttributesTestVisitor visitor = new AttributesTestVisitor();
route.accept(visitor);
assertThat(visitor.visitCount()).isEqualTo(2);
}
}

View File

@ -26,6 +26,7 @@ import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRe
import org.springframework.web.testfixture.server.MockServerWebExchange;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
/**
* @author Arjen Poutsma
@ -126,9 +127,29 @@ public class RouterFunctionTests {
.verify();
}
@Test
public void attributes() {
RouterFunction<ServerResponse> route = RouterFunctions.route(
GET("/atts/1"), request -> ServerResponse.ok().build())
.withAttribute("foo", "bar")
.withAttribute("baz", "qux")
.and(RouterFunctions.route(GET("/atts/2"), request -> ServerResponse.ok().build())
.withAttributes(atts -> {
atts.put("foo", "bar");
atts.put("baz", "qux");
}));
AttributesTestVisitor visitor = new AttributesTestVisitor();
route.accept(visitor);
assertThat(visitor.visitCount()).isEqualTo(2);
}
private Mono<ServerResponse> handlerMethod(ServerRequest request) {
return ServerResponse.ok().bodyValue("42");
}
}

View File

@ -16,6 +16,7 @@
package org.springframework.web.servlet.function;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
@ -59,6 +60,10 @@ class ChangePathPatternParserVisitor implements RouterFunctions.Visitor {
public void resources(Function<ServerRequest, Optional<Resource>> lookupFunction) {
}
@Override
public void attributes(Map<String, Object> attributes) {
}
@Override
public void unknown(RouterFunction<?> routerFunction) {
}

View File

@ -16,7 +16,12 @@
package org.springframework.web.servlet.function;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import org.springframework.util.Assert;
/**
* Represents a function that routes to a {@linkplain HandlerFunction handler function}.
@ -114,4 +119,40 @@ public interface RouterFunction<T extends ServerResponse> {
default void accept(RouterFunctions.Visitor visitor) {
visitor.unknown(this);
}
/**
* Return a new routing function with the given attribute.
* @param name the attribute name
* @param value the attribute value
* @return a function that has the specified attributes
* @since 5.3
*/
default RouterFunction<T> withAttribute(String name, Object value) {
Assert.hasLength(name, "Name must not be empty");
Assert.notNull(value, "Value must not be null");
Map<String, Object> attributes = new LinkedHashMap<>();
attributes.put(name, value);
return new RouterFunctions.AttributesRouterFunction<>(this, attributes);
}
/**
* Return a new routing function with attributes manipulated with the given consumer.
* <p>The map provided to the consumer is "live", so that the consumer can be used
* to {@linkplain Map#put(Object, Object) overwrite} existing attributes,
* {@linkplain Map#remove(Object) remove} attributes, or use any of the other
* {@link Map} methods.
* @param attributesConsumer a function that consumes the attributes map
* @return this builder
* @since 5.3
*/
default RouterFunction<T> withAttributes(Consumer<Map<String, Object>> attributesConsumer) {
Assert.notNull(attributesConsumer, "AttributesConsumer must not be null");
Map<String, Object> attributes = new LinkedHashMap<>();
attributesConsumer.accept(attributes);
return new RouterFunctions.AttributesRouterFunction<>(this, attributes);
}
}

View File

@ -18,6 +18,7 @@ package org.springframework.web.servlet.function;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Consumer;
@ -323,6 +324,35 @@ class RouterFunctionBuilder implements RouterFunctions.Builder {
return onError(exceptionType::isInstance, responseProvider);
}
@Override
public RouterFunctions.Builder withAttribute(String name, Object value) {
Assert.hasLength(name, "Name must not be empty");
Assert.notNull(value, "Value must not be null");
if (this.routerFunctions.isEmpty()) {
throw new IllegalStateException("attributes can only be called after any other method (GET, path, etc.)");
}
int lastIdx = this.routerFunctions.size() - 1;
RouterFunction<ServerResponse> attributed = this.routerFunctions.get(lastIdx)
.withAttribute(name, value);
this.routerFunctions.set(lastIdx, attributed);
return this;
}
@Override
public RouterFunctions.Builder withAttributes(Consumer<Map<String, Object>> attributesConsumer) {
Assert.notNull(attributesConsumer, "AttributesConsumer must not be null");
if (this.routerFunctions.isEmpty()) {
throw new IllegalStateException("attributes can only be called after any other method (GET, path, etc.)");
}
int lastIdx = this.routerFunctions.size() - 1;
RouterFunction<ServerResponse> attributed = this.routerFunctions.get(lastIdx)
.withAttributes(attributesConsumer);
this.routerFunctions.set(lastIdx, attributed);
return this;
}
@Override
public RouterFunction<ServerResponse> build() {
if (this.routerFunctions.isEmpty()) {

View File

@ -16,6 +16,9 @@
package org.springframework.web.servlet.function;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Consumer;
@ -769,6 +772,27 @@ public abstract class RouterFunctions {
Builder onError(Class<? extends Throwable> exceptionType,
BiFunction<Throwable, ServerRequest, ServerResponse> responseProvider);
/**
* Add an attribute with the given name and value to the last route built with this builder.
* @param name the attribute name
* @param value the attribute value
* @return this builder
* @since 5.3
*/
Builder withAttribute(String name, Object value);
/**
* Manipulate the attributes of the last route built with the given consumer.
* <p>The map provided to the consumer is "live", so that the consumer can be used
* to {@linkplain Map#put(Object, Object) overwrite} existing attributes,
* {@linkplain Map#remove(Object) remove} attributes, or use any of the other
* {@link Map} methods.
* @param attributesConsumer a function that consumes the attributes map
* @return this builder
* @since 5.3
*/
Builder withAttributes(Consumer<Map<String, Object>> attributesConsumer);
/**
* Builds the {@code RouterFunction}. All created routes are
* {@linkplain RouterFunction#and(RouterFunction) composed} with one another, and filters
@ -813,6 +837,14 @@ public abstract class RouterFunctions {
*/
void resources(Function<ServerRequest, Optional<Resource>> lookupFunction);
/**
* Receive notification of a router function with attributes. The
* given attributes apply to the router notification that follows this one.
* @param attributes the attributes that apply to the following router
* @since 5.3
*/
void attributes(Map<String, Object> attributes);
/**
* Receive notification of an unknown router function. This method is called for router
* functions that were not created via the various {@link RouterFunctions} methods.
@ -1044,4 +1076,56 @@ public abstract class RouterFunctions {
}
static final class AttributesRouterFunction<T extends ServerResponse> extends AbstractRouterFunction<T> {
private final RouterFunction<T> delegate;
private final Map<String,Object> attributes;
public AttributesRouterFunction(RouterFunction<T> delegate, Map<String, Object> attributes) {
this.delegate = delegate;
this.attributes = initAttributes(attributes);
}
private static Map<String, Object> initAttributes(Map<String, Object> attributes) {
if (attributes.isEmpty()) {
return Collections.emptyMap();
}
else {
return Collections.unmodifiableMap(new LinkedHashMap<>(attributes));
}
}
@Override
public Optional<HandlerFunction<T>> route(ServerRequest request) {
return this.delegate.route(request);
}
@Override
public void accept(Visitor visitor) {
visitor.attributes(this.attributes);
this.delegate.accept(visitor);
}
@Override
public RouterFunction<T> withAttribute(String name, Object value) {
Assert.hasLength(name, "Name must not be empty");
Assert.notNull(value, "Value must not be null");
Map<String, Object> attributes = new LinkedHashMap<>(this.attributes);
attributes.put(name, value);
return new AttributesRouterFunction<>(this.delegate, attributes);
}
@Override
public RouterFunction<T> withAttributes(Consumer<Map<String, Object>> attributesConsumer) {
Assert.notNull(attributesConsumer, "AttributesConsumer must not be null");
Map<String, Object> attributes = new LinkedHashMap<>(this.attributes);
attributesConsumer.accept(attributes);
return new AttributesRouterFunction<>(this.delegate, attributes);
}
}
}

View File

@ -16,6 +16,7 @@
package org.springframework.web.servlet.function;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
@ -68,6 +69,10 @@ class ToStringVisitor implements RouterFunctions.Visitor, RequestPredicates.Visi
this.builder.append(lookupFunction).append('\n');
}
@Override
public void attributes(Map<String, Object> attributes) {
}
@Override
public void unknown(RouterFunction<?> routerFunction) {
indent();

View File

@ -0,0 +1,72 @@
/*
* Copyright 2002-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.function;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
/**
* @author Arjen Poutsma
*/
class AttributesTestVisitor implements RouterFunctions.Visitor {
@Nullable
private Map<String, Object> attributes;
private int visitCount;
public int visitCount() {
return this.visitCount;
}
@Override
public void startNested(RequestPredicate predicate) {
}
@Override
public void endNested(RequestPredicate predicate) {
}
@Override
public void route(RequestPredicate predicate, HandlerFunction<?> handlerFunction) {
assertThat(this.attributes).isNotNull();
this.attributes = null;
}
@Override
public void resources(Function<ServerRequest, Optional<Resource>> lookupFunction) {
}
@Override
public void attributes(Map<String, Object> attributes) {
assertThat(attributes).containsExactly(entry("foo", "bar"), entry("baz", "qux"));
this.attributes = attributes;
this.visitCount++;
}
@Override
public void unknown(RouterFunction<?> routerFunction) {
}
}

View File

@ -214,4 +214,23 @@ class RouterFunctionBuilderTests {
PathPatternsTestUtils.initRequest(httpMethod, null, requestUri, true, consumer), emptyList());
}
@Test
public void attributes() {
RouterFunction<ServerResponse> route = RouterFunctions.route()
.GET("/atts/1", request -> ServerResponse.ok().build())
.withAttribute("foo", "bar")
.withAttribute("baz", "qux")
.GET("/atts/2", request -> ServerResponse.ok().build())
.withAttributes(atts -> {
atts.put("foo", "bar");
atts.put("baz", "qux");
})
.build();
AttributesTestVisitor visitor = new AttributesTestVisitor();
route.accept(visitor);
assertThat(visitor.visitCount()).isEqualTo(2);
}
}

View File

@ -24,6 +24,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.web.servlet.handler.PathPatternsTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.web.servlet.function.RequestPredicates.GET;
/**
* @author Arjen Poutsma
@ -109,7 +110,29 @@ class RouterFunctionTests {
}
@Test
public void attributes() {
RouterFunction<ServerResponse> route = RouterFunctions.route(
GET("/atts/1"), request -> ServerResponse.ok().build())
.withAttribute("foo", "bar")
.withAttribute("baz", "qux")
.and(RouterFunctions.route(GET("/atts/2"), request -> ServerResponse.ok().build())
.withAttributes(atts -> {
atts.put("foo", "bar");
atts.put("baz", "qux");
}));
AttributesTestVisitor visitor = new AttributesTestVisitor();
route.accept(visitor);
assertThat(visitor.visitCount()).isEqualTo(2);
}
private ServerResponse handlerMethod(ServerRequest request) {
return ServerResponse.ok().body("42");
}
}