1

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?

1 Answer 1

2

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();
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.