From e360c22f4aacd81964f44d48209a720251a60146 Mon Sep 17 00:00:00 2001 From: sgibb Date: Tue, 19 Dec 2023 19:12:05 -0500 Subject: [PATCH] Adds support for complex Configurable factory methods. Adds @Configurable to signify that this is a complex config object and use boot binding to create and populate it. Retry and CircuitBreaker are initially supported. Adds SimpleFilterSupplier to ease creation of FilterSupplier objects. Fixes gh-3172 --- .../mvc/common/AbstractGatewayDiscoverer.java | 9 +++-- .../server/mvc/common/Configurable.java | 35 +++++++++++++++++ ...yMvcPropertiesBeanDefinitionRegistrar.java | 38 ++++++++++++++++++- .../mvc/config/NormalizedOperationMethod.java | 6 +++ .../mvc/filter/Bucket4jFilterFunctions.java | 10 ++--- .../filter/CircuitBreakerFilterFunctions.java | 16 +++++--- .../server/mvc/filter/FilterFunctions.java | 10 ++--- .../mvc/filter/RetryFilterFunctions.java | 16 ++++++++ .../mvc/filter/SimpleFilterSupplier.java | 36 ++++++++++++++++++ .../mvc/filter/TokenRelayFilterFunctions.java | 10 ++--- .../server/mvc/handler/HandlerFunctions.java | 4 ++ .../mvc/invoke/reflect/OperationMethod.java | 4 ++ .../main/resources/META-INF/spring.factories | 1 + ...ropertiesBeanDefinitionRegistrarTests.java | 6 +-- .../server/mvc/test/TestFilterSupplier.java | 13 ++----- ...propertiesbeandefinitionregistrartests.yml | 18 ++++++++- 16 files changed, 188 insertions(+), 44 deletions(-) create mode 100644 spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/Configurable.java create mode 100644 spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/SimpleFilterSupplier.java diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/AbstractGatewayDiscoverer.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/AbstractGatewayDiscoverer.java index 3a04733553..0021181daa 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/AbstractGatewayDiscoverer.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/AbstractGatewayDiscoverer.java @@ -48,9 +48,12 @@ public >, R> void doDiscover(Class supp } catch (NoClassDefFoundError e) { if (log.isDebugEnabled()) { - log.debug(LogMessage.format("NoClassDefFoundError discovering supplier %s for type %s", supplierClass, returnType)); - } else if (log.isTraceEnabled()) { - log.debug(LogMessage.format("NoClassDefFoundError discovering supplier %s for type %s", supplierClass, returnType), e); + log.debug(LogMessage.format("NoClassDefFoundError discovering supplier %s for type %s", + supplierClass, returnType)); + } + else if (log.isTraceEnabled()) { + log.debug(LogMessage.format("NoClassDefFoundError discovering supplier %s for type %s", + supplierClass, returnType), e); } } } diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/Configurable.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/Configurable.java new file mode 100644 index 0000000000..b20b8b3c30 --- /dev/null +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/Configurable.java @@ -0,0 +1,35 @@ +/* + * Copyright 2013-2023 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.cloud.gateway.server.mvc.common; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.stereotype.Indexed; + +@Target({ ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Indexed +public @interface Configurable { + + Class value() default Void.class; + +} diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/GatewayMvcPropertiesBeanDefinitionRegistrar.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/GatewayMvcPropertiesBeanDefinitionRegistrar.java index 90b9c62f05..099c64b81d 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/GatewayMvcPropertiesBeanDefinitionRegistrar.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/GatewayMvcPropertiesBeanDefinitionRegistrar.java @@ -19,6 +19,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; @@ -32,13 +33,19 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.boot.context.properties.bind.Bindable; import org.springframework.boot.context.properties.bind.Binder; +import org.springframework.boot.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler; +import org.springframework.boot.context.properties.source.ConfigurationPropertySource; +import org.springframework.boot.context.properties.source.MapConfigurationPropertySource; +import org.springframework.cloud.gateway.server.mvc.common.Configurable; import org.springframework.cloud.gateway.server.mvc.common.MvcUtils; import org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions; import org.springframework.cloud.gateway.server.mvc.filter.FilterDiscoverer; import org.springframework.cloud.gateway.server.mvc.handler.HandlerDiscoverer; import org.springframework.cloud.gateway.server.mvc.invoke.InvocationContext; import org.springframework.cloud.gateway.server.mvc.invoke.OperationArgumentResolver; +import org.springframework.cloud.gateway.server.mvc.invoke.OperationParameter; import org.springframework.cloud.gateway.server.mvc.invoke.OperationParameters; import org.springframework.cloud.gateway.server.mvc.invoke.ParameterValueMapper; import org.springframework.cloud.gateway.server.mvc.invoke.convert.ConversionServiceParameterValueMapper; @@ -46,6 +53,7 @@ import org.springframework.cloud.gateway.server.mvc.invoke.reflect.ReflectiveOperationInvoker; import org.springframework.cloud.gateway.server.mvc.predicate.PredicateDiscoverer; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; +import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.env.Environment; import org.springframework.core.log.LogMessage; import org.springframework.core.type.AnnotationMetadata; @@ -264,6 +272,10 @@ private Optional findOperation(MultiValueMap args) { Map normalizedArgs = operationMethod.getNormalizedArgs(); OperationParameters parameters = operationMethod.getParameters(); + if (operationMethod.isConfigurable()) { + // this is a special case + return true; + } if (parameters.getParameterCount() != normalizedArgs.size()) { return false; } @@ -277,13 +289,37 @@ private static boolean matchOperation(NormalizedOperationMethod operationMethod, } private T invokeOperation(OperationMethod operationMethod, Map operationArgs) { - Map args = new HashMap<>(operationArgs); + Map args = new HashMap<>(); + if (operationMethod.isConfigurable()) { + OperationParameter operationParameter = operationMethod.getParameters().get(0); + Object config = bindConfigurable(operationMethod, args, operationParameter); + args.put(operationParameter.getName(), config); + } + else { + args.putAll(operationArgs); + } ReflectiveOperationInvoker operationInvoker = new ReflectiveOperationInvoker(operationMethod, this.parameterValueMapper); InvocationContext context = new InvocationContext(args, trueNullOperationArgumentResolver); return operationInvoker.invoke(context); } + private static Object bindConfigurable(OperationMethod operationMethod, Map args, + OperationParameter operationParameter) { + Class configurableType = operationParameter.getType(); + Configurable configurable = operationMethod.getMethod().getAnnotation(Configurable.class); + if (configurable != null && !configurable.value().equals(Void.class)) { + configurableType = configurable.value(); + } + Bindable bindable = Bindable.of(configurableType); + List propertySources = Collections + .singletonList(new MapConfigurationPropertySource(args)); + // TODO: potentially deal with conversion service + Binder binder = new Binder(propertySources, null, DefaultConversionService.getSharedInstance()); + Object config = binder.bindOrCreate("", bindable, new IgnoreTopLevelConverterNotFoundBindHandler()); + return config; + } + static class TrueNullOperationArgumentResolver implements OperationArgumentResolver { @Override diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/NormalizedOperationMethod.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/NormalizedOperationMethod.java index 36c3d944a6..7ca9ed542a 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/NormalizedOperationMethod.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/NormalizedOperationMethod.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Map; +import org.springframework.cloud.gateway.server.mvc.common.Configurable; import org.springframework.cloud.gateway.server.mvc.common.NameUtils; import org.springframework.cloud.gateway.server.mvc.common.Shortcut; import org.springframework.cloud.gateway.server.mvc.invoke.OperationParameter; @@ -50,6 +51,11 @@ public Method getMethod() { return delegate.getMethod(); } + public boolean isConfigurable() { + Configurable annotation = delegate.getMethod().getAnnotation(Configurable.class); + return annotation != null && delegate.getParameters().getParameterCount() == 1; + } + @Override public OperationParameters getParameters() { return delegate.getParameters(); diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/Bucket4jFilterFunctions.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/Bucket4jFilterFunctions.java index 900f040104..48cdc6ade0 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/Bucket4jFilterFunctions.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/Bucket4jFilterFunctions.java @@ -16,10 +16,7 @@ package org.springframework.cloud.gateway.server.mvc.filter; -import java.lang.reflect.Method; import java.time.Duration; -import java.util.Arrays; -import java.util.Collection; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; @@ -189,11 +186,10 @@ public RateLimitConfig setHeaderName(String headerName) { } - static class FilterSupplier implements org.springframework.cloud.gateway.server.mvc.filter.FilterSupplier { + public static class FilterSupplier extends SimpleFilterSupplier { - @Override - public Collection get() { - return Arrays.asList(Bucket4jFilterFunctions.class.getMethods()); + public FilterSupplier() { + super(Bucket4jFilterFunctions.class); } } diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/CircuitBreakerFilterFunctions.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/CircuitBreakerFilterFunctions.java index 71db08e9ac..7018539a31 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/CircuitBreakerFilterFunctions.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/CircuitBreakerFilterFunctions.java @@ -17,10 +17,8 @@ package org.springframework.cloud.gateway.server.mvc.filter; import java.io.IOException; -import java.lang.reflect.Method; import java.net.URI; import java.util.Arrays; -import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Set; @@ -32,6 +30,7 @@ import org.springframework.cloud.client.circuitbreaker.CircuitBreaker; import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory; +import org.springframework.cloud.gateway.server.mvc.common.Configurable; import org.springframework.cloud.gateway.server.mvc.common.HttpStatusHolder; import org.springframework.cloud.gateway.server.mvc.common.MvcUtils; import org.springframework.cloud.gateway.server.mvc.common.Shortcut; @@ -66,6 +65,12 @@ public static HandlerFilterFunction circuitBreak Consumer configConsumer) { CircuitBreakerConfig config = new CircuitBreakerConfig(); configConsumer.accept(config); + return circuitBreaker(config); + } + + @Shortcut + @Configurable + public static HandlerFilterFunction circuitBreaker(CircuitBreakerConfig config) { Set failureStatuses = config.getStatusCodes().stream() .map(status -> HttpStatusHolder.valueOf(status).resolve()).collect(Collectors.toSet()); return (request, next) -> { @@ -191,11 +196,10 @@ public CircuitBreakerStatusCodeException(HttpStatusCode statusCode) { } - public static class FilterSupplier implements org.springframework.cloud.gateway.server.mvc.filter.FilterSupplier { + public static class FilterSupplier extends SimpleFilterSupplier { - @Override - public Collection get() { - return Arrays.asList(CircuitBreakerFilterFunctions.class.getMethods()); + public FilterSupplier() { + super(CircuitBreakerFilterFunctions.class); } } diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FilterFunctions.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FilterFunctions.java index 5a22ade6d1..31f490537b 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FilterFunctions.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FilterFunctions.java @@ -16,10 +16,7 @@ package org.springframework.cloud.gateway.server.mvc.filter; -import java.lang.reflect.Method; import java.net.URI; -import java.util.Arrays; -import java.util.Collection; import java.util.function.Consumer; import org.springframework.cloud.gateway.server.mvc.common.HttpStatusHolder; @@ -228,11 +225,10 @@ static HandlerFilterFunction setStatus(HttpStatu return ofResponseProcessor(AfterFilterFunctions.setStatus(statusCode)); } - class FilterSupplier implements org.springframework.cloud.gateway.server.mvc.filter.FilterSupplier { + class FilterSupplier extends SimpleFilterSupplier { - @Override - public Collection get() { - return Arrays.asList(FilterFunctions.class.getMethods()); + public FilterSupplier() { + super(FilterFunctions.class); } } diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/RetryFilterFunctions.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/RetryFilterFunctions.java index 91380c903f..c7fd9acb5e 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/RetryFilterFunctions.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/RetryFilterFunctions.java @@ -26,6 +26,8 @@ import java.util.concurrent.TimeoutException; import java.util.function.Consumer; +import org.springframework.cloud.gateway.server.mvc.common.Configurable; +import org.springframework.cloud.gateway.server.mvc.common.Shortcut; import org.springframework.core.NestedRuntimeException; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; @@ -53,6 +55,12 @@ public static HandlerFilterFunction retry(int re public static HandlerFilterFunction retry(Consumer configConsumer) { RetryConfig config = new RetryConfig(); configConsumer.accept(config); + return retry(config); + } + + @Shortcut + @Configurable + public static HandlerFilterFunction retry(RetryConfig config) { RetryTemplateBuilder retryTemplateBuilder = RetryTemplate.builder(); CompositeRetryPolicy compositeRetryPolicy = new CompositeRetryPolicy(); Map, Boolean> retryableExceptions = new HashMap<>(); @@ -191,4 +199,12 @@ public ServerResponse getResponse() { } + public static class FilterSupplier extends SimpleFilterSupplier { + + public FilterSupplier() { + super(RetryFilterFunctions.class); + } + + } + } diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/SimpleFilterSupplier.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/SimpleFilterSupplier.java new file mode 100644 index 0000000000..7eb0d13aaf --- /dev/null +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/SimpleFilterSupplier.java @@ -0,0 +1,36 @@ +/* + * Copyright 2013-2023 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.cloud.gateway.server.mvc.filter; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collection; + +public class SimpleFilterSupplier implements FilterSupplier { + + private final Class filtersClass; + + public SimpleFilterSupplier(Class filtersClass) { + this.filtersClass = filtersClass; + } + + @Override + public Collection get() { + return Arrays.asList(filtersClass.getMethods()); + } + +} diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/TokenRelayFilterFunctions.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/TokenRelayFilterFunctions.java index b1b6f18852..a9d0d67c34 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/TokenRelayFilterFunctions.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/TokenRelayFilterFunctions.java @@ -16,10 +16,7 @@ package org.springframework.cloud.gateway.server.mvc.filter; -import java.lang.reflect.Method; import java.security.Principal; -import java.util.Arrays; -import java.util.Collection; import org.springframework.cloud.gateway.server.mvc.common.Shortcut; import org.springframework.security.oauth2.client.OAuth2AuthorizeRequest; @@ -58,11 +55,10 @@ public static HandlerFilterFunction tokenRelay() }; } - class FilterSupplier implements org.springframework.cloud.gateway.server.mvc.filter.FilterSupplier { + public static class FilterSupplier extends SimpleFilterSupplier { - @Override - public Collection get() { - return Arrays.asList(TokenRelayFilterFunctions.class.getMethods()); + public FilterSupplier() { + super(TokenRelayFilterFunctions.class); } } diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/HandlerFunctions.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/HandlerFunctions.java index c992934951..b6f358e159 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/HandlerFunctions.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/HandlerFunctions.java @@ -73,6 +73,10 @@ public static HandlerFunction http() { return new LookupProxyExchangeHandlerFunction(); } + public static HandlerFunction no() { + return http(); + } + static class LookupProxyExchangeHandlerFunction implements HandlerFunction { private final URI uri; diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/invoke/reflect/OperationMethod.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/invoke/reflect/OperationMethod.java index 344993fa29..58a1b3e785 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/invoke/reflect/OperationMethod.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/invoke/reflect/OperationMethod.java @@ -26,4 +26,8 @@ public interface OperationMethod { OperationParameters getParameters(); + default boolean isConfigurable() { + return false; + } + } diff --git a/spring-cloud-gateway-server-mvc/src/main/resources/META-INF/spring.factories b/spring-cloud-gateway-server-mvc/src/main/resources/META-INF/spring.factories index b060444c13..972cdb6d78 100644 --- a/spring-cloud-gateway-server-mvc/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-gateway-server-mvc/src/main/resources/META-INF/spring.factories @@ -1,6 +1,7 @@ org.springframework.cloud.gateway.server.mvc.filter.FilterSupplier=\ org.springframework.cloud.gateway.server.mvc.filter.Bucket4jFilterFunctions.FilterSupplier,\ org.springframework.cloud.gateway.server.mvc.filter.CircuitBreakerFilterFunctions.FilterSupplier,\ + org.springframework.cloud.gateway.server.mvc.filter.RetryFilterFunctions.FilterSupplier,\ org.springframework.cloud.gateway.server.mvc.filter.TokenRelayFilterFunctions.FilterSupplier,\ org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions.FilterSupplier diff --git a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/config/GatewayMvcPropertiesBeanDefinitionRegistrarTests.java b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/config/GatewayMvcPropertiesBeanDefinitionRegistrarTests.java index d8f2bef711..7d4c3f0fc5 100644 --- a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/config/GatewayMvcPropertiesBeanDefinitionRegistrarTests.java +++ b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/config/GatewayMvcPropertiesBeanDefinitionRegistrarTests.java @@ -65,7 +65,7 @@ void contextLoads(ApplicationContext context) { Map routerFunctions = getRouterFunctions(context); assertThat(routerFunctions).hasSizeGreaterThanOrEqualTo(5).containsKeys("listRoute1", "route1", - "route2CustomId", "listRoute2", "listRoute3"); + "route2CustomId", "listRoute2", "listRoute3", "listRoute4"); RouterFunction listRoute1RouterFunction = routerFunctions.get("listRoute1"); listRoute1RouterFunction.accept(new AbstractRouterFunctionsVisitor() { @Override @@ -176,7 +176,7 @@ public void lbRouteWorks() { @SuppressWarnings({ "rawtypes", "unchecked" }) void refreshWorks(ConfigurableApplicationContext context) { Map routerFunctions = getRouterFunctions(context); - assertThat(routerFunctions).hasSize(5); + assertThat(routerFunctions).hasSize(6); TestPropertyValues.of("spring.cloud.gateway.mvc.routesMap.route3.uri=https://example3.com", "spring.cloud.gateway.mvc.routesMap.route3.predicates[0].name=Path", "spring.cloud.gateway.mvc.routesMap.route3.predicates[0].args.pattern=/anything/mapRoute3", @@ -196,7 +196,7 @@ void refreshWorks(ConfigurableApplicationContext context) { GatewayMvcProperties properties = context.getBean(GatewayMvcProperties.class); assertThat(properties.getRoutesMap()).hasSize(3).containsKey("route3"); routerFunctions = getRouterFunctions(context); - assertThat(routerFunctions).hasSize(6); + assertThat(routerFunctions).hasSize(7); } @SpringBootConfiguration diff --git a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/test/TestFilterSupplier.java b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/test/TestFilterSupplier.java index 41ad6a769c..ce940d2a41 100644 --- a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/test/TestFilterSupplier.java +++ b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/test/TestFilterSupplier.java @@ -16,19 +16,14 @@ package org.springframework.cloud.gateway.server.mvc.test; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.Collection; - -import org.springframework.cloud.gateway.server.mvc.filter.FilterSupplier; +import org.springframework.cloud.gateway.server.mvc.filter.SimpleFilterSupplier; import org.springframework.web.servlet.function.HandlerFilterFunction; import org.springframework.web.servlet.function.ServerResponse; -public class TestFilterSupplier implements FilterSupplier { +public class TestFilterSupplier extends SimpleFilterSupplier { - @Override - public Collection get() { - return Arrays.asList(TestFilterSupplier.class.getMethods()); + public TestFilterSupplier() { + super(TestFilterSupplier.class); } public static HandlerFilterFunction localServerPortUriResolver() { diff --git a/spring-cloud-gateway-server-mvc/src/test/resources/application-propertiesbeandefinitionregistrartests.yml b/spring-cloud-gateway-server-mvc/src/test/resources/application-propertiesbeandefinitionregistrartests.yml index e7cdc876cf..34b1726be5 100644 --- a/spring-cloud-gateway-server-mvc/src/test/resources/application-propertiesbeandefinitionregistrartests.yml +++ b/spring-cloud-gateway-server-mvc/src/test/resources/application-propertiesbeandefinitionregistrartests.yml @@ -26,7 +26,7 @@ spring.cloud.gateway.mvc: - HttpbinUriResolver= - AddRequestHeader=X-Test,listRoute1 - id: listRoute2 - uri: https://examplel2.com + uri: no://op predicates: - Method=GET,POST - Path=/anything/listRoute2 @@ -44,6 +44,22 @@ spring.cloud.gateway.mvc: args: name: X-Test values: listRoute3 + - id: listRoute4 + uri: https://example1.com + predicates: + - Path=/anything/example1 + filters: + - name: Retry + args: + retries: 3 + series: SERVER_ERROR + methods: GET,POST + - name: CircuitBreaker + args: + id: mycb + statusCodes: 500,501 + fallbackpath: GET,POST + logging: level: org.springframework.cloud.gateway.server.mvc: TRACE