Search code examples
amazon-web-servicesamazon-elastic-beanstalkspring-cloudspring-cloud-gateway

Spring Cloud Gateway in AWS not routing correctly


I have created a simple REST service as below:

@RestController
public class ServiceController {

    @GetMapping(value = "/getSimpleResponse")
    public ResponseEntity<String> getSimpleResponse() {
        return new ResponseEntity<>("Hello from Service 1", HttpStatus.OK);
    }
}

I deploy this to AWS elastic beanstalk and is working fine. Here: Service 1 in AWS

Now I create a Spring Cloud Gateway service with route configuration as below:

@Configuration
public class GatewayConfig {
    @Bean
    public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(getFirstTestServiceRoute())
                .build();
    }

    private Function<PredicateSpec, Route.AsyncBuilder> getFirstTestServiceRoute() {
        return p -> p
                .path("/service1/**")
                .uri("http://testservice1-env.qgwe5a4qq8.ap-south-1.elasticbeanstalk.com/")
                .id("service1");
    }
}

I Deploy the Gateway Application in AWS Elastic beanstalk here.

But the GatewayURL/service1/getSimpleResponse returns a Whitelabel Error Page. I was expecting the same output as Service 1. What am I doing wrong?


Solution

  • I was missing RequestMapping at the class level. My URL format is

    GatewayURL/service1/getSimpleResponse

    I assume this is because the Spring Cloud Gateway is probably appending the complete path(/service1/getSimpleResponse) to the downstream service URL

     @RestController
        @RequestMapping("/service1")
        public class ServiceController {
    
            @GetMapping(value = "/getSimpleResponse")
            public ResponseEntity<String> getSimpleResponse() {
                return new ResponseEntity<>("Hello from Service 1", HttpStatus.OK);
            }
        }