Home > Back-end >  spring cloud gateway only forwards to base path of url
spring cloud gateway only forwards to base path of url

Time:03-23

I'm trying to build a simple api-gateway using spring-cloud-gateway. So far I understood the basic principle but I'm running in a specific Problem:

The target url which I'm forwarding my request potentially contains zero, one or multiple path segments. Unfortunately, these path segments are ignored.


private final String routingTargetWithPath = "http://hapi.fhir.org/baseR4";

  @Bean
  public RouteLocator routeLocator(RouteLocatorBuilder builder) {
    return builder.routes()
        .route("patient", r -> r
            .path("/Patient", "/Patient/*")
            .and()
            .method(GET)
            .uri(routingTargetWithPath)
        )
        .build();
  }

Using curl sending the request to my api-gateway:

curl http://localhost:8080/Patient
  and accordingly
curl http://localhost:8080/Patient/2069748

I would assume the requests would be routed to:

http://hapi.fhir.org/baseR4/Patient
  and accordingly
http://hapi.fhir.org/baseR4/Patient/2069748

But instead they are being routed to:

http://hapi.fhir.org/Patient
  and accordingly
http://hapi.fhir.org/Patient/2069748

So, the path of the configured routing url is ignored. Unfortunately, I can't do a manual rewrite here, since in production the "routingTarget" will be configured and I don't know, if and how many path segments it will contain.

How can I achieve to route to the complete configured routing target?

CodePudding user response:

Ok, I found the answer: According to here it is intentional, that the path of the uri is ignored. So in my case, the set path filter would fix the problem:

private final URI routingTargetWithPath = URI.create("http://hapi.fhir.org/baseR4");

  @Bean
  public RouteLocator routeLocator(RouteLocatorBuilder builder) {
    return builder.routes()
        .route("patient", r -> r
            .path("/Patient", "/Patient/*")
            .and()
            .method(GET)
            .filters(f -> f.prefixPath(routingTargetWithPath.getPath()))
            .uri(routingTargetWithPath)
        )
        .build();
  }
  • Related