Version
- java: 11
- Spring Boot : 2.6.9
- Spring Cloud : 2021.0.3
Describe the bug
Exact same issue #1764 report from Aug.2020 by @Bryksin
Since @spencergibb guided to update Spring Cloud version to hoxton.sr5 will solve the problem, BUT I am using the version 2021.0.3 which is further updated then hoxton.sr5 and still has the same issue.
Cloud Gateway is not forwarding requests, but even worse it just returns an empty body of status code 200 even for invalid or non-existing endpoints.
The reason for it is because I set the uri configuration without the URI scheme in it
# application.yml
spring:
cloud:
gateway:
routes:
- id: test
uri: localhost:8080
predicates:
- Path=/api/v?/test
if I correct my configuration to
# fixed ver of application.yml
spring:
cloud:
gateway:
routes:
- id: test
uri: http://localhost:8080 # prepended http://
predicates:
- Path=/api/v?/test
Then it works fine.
Sample

FYI.. took 6m40s to get response is because I set the breakpoint and debug, nothing related to performance issue
Trouble Shoot
As @cstraw01 said from #1764
while on bootstrap, URI in RouteDefinition gets initialized with scheme of localhost

After that, RouteToRequestUrlFilter merge an url from URL instance which has invalid scheme and put it as an attribute of exchange GATEWAY_REQUEST_URL_ATTR

Followed by filter chain, it will move on to NettyRoutingFilter from where request should be forwarded, but since GATEWAY_REQUEST_URL_ATTR in exchange attribute is in invalid format so it won't move on.


For now on, I added custom filter in my project to throws an error if http:// is not prepended on uri configuration of route to fix current problem, but is its just an temporarily solution.
@Component
public class RouteDefinitionFilter implements GlobalFilter, Ordered {
private static final int ORDER = NettyRoutingFilter.ORDER - 1;
private static final String LOCALHOST = "localhost";
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
URI requestUrl = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
if (requestUrl.getScheme().equals(LOCALHOST)) {
throw new IllegalArgumentException(
String.format("Unexpected uri scheme for path: \"%s\"", requestUrl.getPath())
);
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return ORDER;
}
}
Suggestion
I have checked that If uri configuration starts with localhost, host is always null

In RouteToRequestUrlFilter, if scheme of uri is lb it checks whether host is null or not
|
if ("lb".equalsIgnoreCase(routeUri.getScheme()) && routeUri.getHost() == null) { |
|
// Load balanced URIs should always have a host. If the host is null it is |
|
// most |
|
// likely because the host name was invalid (for example included an |
|
// underscore) |
|
throw new IllegalStateException("Invalid host: " + routeUri.toString()); |
|
} |
since valid URI must contains non-null host part so I would like to change code like...
/*
if ("lb".equalsIgnoreCase(routeUri.getScheme()) && routeUri.getHost() == null) {
// Load balanced URIs should always have a host. If the host is null it is
// most
// likely because the host name was invalid (for example included an
// underscore)
throw new IllegalStateException("Invalid host: " + routeUri.toString());
}
*/
// not just when scheme is lb, always checks host is non-null
if (routeUri.getHost() == null) {
throw new IllegalStateException("Invalid host: " + routeUri.toString());
}
If so, it could also filter my situation as when uri configuration is given as localhost:8080
thank you :)
Version
Describe the bug
Exact same issue #1764 report from Aug.2020 by @Bryksin
Since @spencergibb guided to update Spring Cloud version to hoxton.sr5 will solve the problem, BUT I am using the version 2021.0.3 which is further updated then hoxton.sr5 and still has the same issue.
Cloud Gateway is not forwarding requests, but even worse it just returns an empty body of status code 200 even for invalid or non-existing endpoints.
The reason for it is because I set the
uriconfiguration without the URI scheme in itif I correct my configuration to
Then it works fine.
Sample
FYI.. took 6m40s to get response is because I set the breakpoint and debug, nothing related to performance issue
Trouble Shoot
As @cstraw01 said from #1764
while on bootstrap,
URIinRouteDefinitiongets initialized with scheme oflocalhostAfter that,
RouteToRequestUrlFiltermerge an url from URL instance which has invalid scheme and put it as an attribute of exchangeGATEWAY_REQUEST_URL_ATTRFollowed by filter chain, it will move on to
NettyRoutingFilterfrom where request should be forwarded, but sinceGATEWAY_REQUEST_URL_ATTRin exchange attribute is in invalid format so it won't move on.For now on, I added custom filter in my project to throws an error if
http://is not prepended onuriconfiguration of route to fix current problem, but is its just an temporarily solution.Suggestion
I have checked that If
uriconfiguration starts withlocalhost,hostis always nullIn RouteToRequestUrlFilter, if scheme of uri is
lbit checks whether host is null or notspring-cloud-gateway/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilter.java
Lines 80 to 86 in 84fd7cb
since valid URI must contains non-null host part so I would like to change code like...
If so, it could also filter my situation as when uri configuration is given as
localhost:8080thank you :)