Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Cloud Gateway, how to programatically get the matched route information

For a sample route definition below, when the control goes to MyCustomFilter, Is there any way to get the following information

  1. log the route id that is matched (in this case it is "test-route")
  2. log absolute value of the URI of this route definition that is matched (http://my-downstream-service.com)

Route Definition:

  - id: test-route
    uri: http://my-downstream-service.com
    predicates:
      - Path=/v3/status
      - Method=POST
    filters:
      - MyCustomFilter
like image 221
Santosh Ksl Avatar asked Sep 04 '25 01:09

Santosh Ksl


1 Answers

All the matched route information like id, URI, predicates & filters are available in org.springframework.cloud.gateway.route.Route, all we need to do is to get the handle of the Route object using the below statement in any of the filters.

org.springframework.cloud.gateway.route.Route route = exchange.getAttribute(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);

and get the required information like route.getId()

like image 123
Santosh Ksl Avatar answered Sep 07 '25 18:09

Santosh Ksl