I am unable to forward my request to a URI with contextpath.
For example I am requesting my Spring cloud gateway server with
And I expect it to forward to
But It gives me 404
@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("api-server-date", r -> r.host("**") .and() .path("/mygateway/server/date") .filters(f -> f.stripPrefix(1)) .uri("")) .build(); } It works when I change my configuration to this
@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("api-server-date", r -> r.host("**") .and() .path("/mygateway/xyzapi/v1/server/date") .filters(f -> f.stripPrefix(1)) .uri("")) .build(); } I don't know what configuration I am providing wrong. It should work.
All I want is to use
As my URI. Meaning all Incoming traffic to my spring cloud gateway server should forward to this URL after appending the incoming url
Kindly suggest what is wrong ? Or is it something not possible with Spring Cloud Gateway ?
21 Answer
I think what you want (looking at online samples) is:
@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { String requestHandlerUrl = ""; return builder.routes() .route("api-server-date", r -> r.host("**") .and() .path("/mygateway/server/date") .filters(f -> f.stripPrefix(1)) .uri(requestHandlerUrl) .build(); } ... so that requests made to whatever host your app is deployed on are forwarded/passed to requestHandlerUrl as the destination.
It looks as if /mygateway/server/date is the "shortcut" or alias for the URL you actually want to handle the request.