diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/actuate/AbstractGatewayControllerEndpoint.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/actuate/AbstractGatewayControllerEndpoint.java index 3e1d81da4..fd31bf68b 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/actuate/AbstractGatewayControllerEndpoint.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/actuate/AbstractGatewayControllerEndpoint.java @@ -174,15 +174,15 @@ public class AbstractGatewayControllerEndpoint implements ApplicationEventPublis }); return Flux.fromIterable(routes) - .flatMap(routeDefinition -> this.routeDefinitionWriter.save(Mono.just(routeDefinition).map(r -> { - log.debug("Saving route: " + routeDefinition); - return r; - }))).then(Mono.defer(() -> Mono.just(ResponseEntity.ok().build()))) - .switchIfEmpty(Mono.defer(() -> Mono.just(ResponseEntity.badRequest().build()))); + .flatMap(routeDefinition -> this.routeDefinitionWriter.save(Mono.just(routeDefinition).map(r -> { + log.debug("Saving route: " + routeDefinition); + return r; + }))).then(Mono.defer(() -> Mono.just(ResponseEntity.ok().build()))) + .switchIfEmpty(Mono.defer(() -> Mono.just(ResponseEntity.badRequest().build()))); } private void validateRouteId(RouteDefinition routeDefinition) { - if(routeDefinition.getId() == null) { + if (routeDefinition.getId() == null) { handleError("Saving multiple routes require specifying the ID for every route"); } } diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/actuate/GatewayControllerEndpointTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/actuate/GatewayControllerEndpointTests.java index d17d4d355..17337172a 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/actuate/GatewayControllerEndpointTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/actuate/GatewayControllerEndpointTests.java @@ -339,19 +339,17 @@ public class GatewayControllerEndpointTests { PredicateDefinition hostRoutePredicateDefinition2 = new PredicateDefinition("Host=myhost-2.org"); PredicateDefinition methodRoutePredicateDefinition2 = new PredicateDefinition("Method=GET"); PredicateDefinition testPredicateDefinition2 = new PredicateDefinition("Test=value-2"); - testRouteDefinition2.setPredicates( - Arrays.asList(hostRoutePredicateDefinition2, methodRoutePredicateDefinition2, testPredicateDefinition2) - ); + testRouteDefinition2.setPredicates(Arrays.asList(hostRoutePredicateDefinition2, methodRoutePredicateDefinition2, + testPredicateDefinition2)); List multipleRouteDefs = List.of(testRouteDefinition, testRouteDefinition2); testClient.post().uri("http://localhost:" + port + "/actuator/gateway/routes") - .accept(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(multipleRouteDefs)).exchange() - .expectStatus().isOk(); + .accept(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(multipleRouteDefs)).exchange() + .expectStatus().isOk(); testClient.get().uri("http://localhost:" + port + "/actuator/gateway/routedefinitions") - .accept(MediaType.APPLICATION_JSON).exchange() - .expectBody() - .jsonPath("[?(@.id in ['%s','%s'])].id".formatted(routeId1, routeId2)).exists(); + .accept(MediaType.APPLICATION_JSON).exchange().expectBody() + .jsonPath("[?(@.id in ['%s','%s'])].id".formatted(routeId1, routeId2)).exists(); } @Test @@ -382,15 +380,14 @@ public class GatewayControllerEndpointTests { List multipleRouteDefs = List.of(testRouteDefinition, testRouteDefinition2); testClient.post().uri("http://localhost:" + port + "/actuator/gateway/routes") - .accept(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(multipleRouteDefs)).exchange() - .expectStatus().is4xxClientError(); + .accept(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(multipleRouteDefs)).exchange() + .expectStatus().is4xxClientError(); testClient.get().uri("http://localhost:" + port + "/actuator/gateway/routedefinitions") - .accept(MediaType.APPLICATION_JSON).exchange() - .expectBody() - .jsonPath("[?(@.id in ['%s','%s'])].id".formatted(routeId1, routeId2)).doesNotExist(); + .accept(MediaType.APPLICATION_JSON).exchange().expectBody() + .jsonPath("[?(@.id in ['%s','%s'])].id".formatted(routeId1, routeId2)).doesNotExist(); } - + @Test public void testPostValidShortcutRouteDefinition() { RouteDefinition testRouteDefinition = new RouteDefinition();