Spring Cloud - Testing Gateway Application



Now in order to test gateway application created in Spring Cloud - Creating Gateway Application chapter. We required the following application running prior to gateway application.

Testing Gateway application

Now run the Gateway application as Run as > Spring Boot App in Eclipse and it will show the following output in console.


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

[32m :: Spring Boot :: [39m              [2m (v3.5.6)[0;39m

[2m2025-10-09T10:21:43.941+05:30[0;39m [32m INFO[0;39m [35m40680[0;39m [2m--- [restaurant-gateway-service] [           main] [0;39m[36mc.t.gateway.GatewayApplication          [0;39m [2m:[0;39m Starting GatewayApplication using Java 21.0.6 with PID 40680 (D:\Projects\gateway\target\classes started by mahes in D:\Projects\gateway)
[2m2025-10-09T10:21:43.943+05:30[0;39m [32m INFO[0;39m [35m40680[0;39m [2m--- [restaurant-gateway-service] [           main] [0;39m[36mc.t.gateway.GatewayApplication          [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
[2m2025-10-09T10:21:44.687+05:30[0;39m [32m INFO[0;39m [35m40680[0;39m [2m--- [restaurant-gateway-service] [           main] [0;39m[36mo.s.cloud.context.scope.GenericScope    [0;39m [2m:[0;39m BeanFactory id=dc957f04-6ca7-30aa-8917-3f2c108d81b3
[2m2025-10-09T10:21:45.312+05:30[0;39m [32m INFO[0;39m [35m40680[0;39m [2m--- [restaurant-gateway-service] [           main] [0;39m[36mo.s.c.g.r.RouteDefinitionRouteLocator   [0;39m [2m:[0;39m Loaded RoutePredicateFactory [After]
...
[2m2025-10-09T10:21:45.518+05:30[0;39m [32m INFO[0;39m [35m40680[0;39m [2m--- [restaurant-gateway-service] [           main] [0;39m[36mDiscoveryClientOptionalArgsConfiguration[0;39m [2m:[0;39m Eureka HTTP Client uses RestTemplate.
[2m2025-10-09T10:21:45.547+05:30[0;39m [33m WARN[0;39m [35m40680[0;39m [2m--- [restaurant-gateway-service] [           main] 
[2m2025-10-09T10:21:45.958+05:30[0;39m [32m INFO[0;39m [35m40680[0;39m [2m--- [restaurant-gateway-service] [           main] [0;39m[36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Discovery Client initialized at timestamp 1759985505957 with initial instances count: 1
[2m2025-10-09T10:21:45.960+05:30[0;39m [32m INFO[0;39m [35m40680[0;39m [2m--- [restaurant-gateway-service] [           main] [0;39m[36mo.s.c.n.e.s.EurekaServiceRegistry       [0;39m [2m:[0;39m Registering application RESTAURANT-GATEWAY-SERVICE with eureka with status UP
[2m2025-10-09T10:21:45.960+05:30[0;39m [32m INFO[0;39m [35m40680[0;39m [2m--- [restaurant-gateway-service] [           main] [0;39m[36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Saw local status change event StatusChangeEvent [timestamp=1759985505960, current=UP, previous=STARTING]
[2m2025-10-09T10:21:45.962+05:30[0;39m [32m INFO[0;39m [35m40680[0;39m [2m--- [restaurant-gateway-service] [foReplicator-%d] [0;39m[36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m DiscoveryClient_RESTAURANT-GATEWAY-SERVICE/Home:restaurant-gateway-service:8084: registering service...
[2m2025-10-09T10:21:46.002+05:30[0;39m [32m INFO[0;39m [35m40680[0;39m [2m--- [restaurant-gateway-service] [foReplicator-%d] [0;39m[36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m DiscoveryClient_RESTAURANT-GATEWAY-SERVICE/Home:restaurant-gateway-service:8084 - registration status: 204

Once this is done, we have our Gateway ready to be tested on port 8084. Lets first hit http://localhost:8084/customer/1 and we see the request is correctly routed to Customer Service and we get the following output −

{
   "id": 1,
   "name": "Jane",
   "city": "DC"
}

And now, hit our restaurant API, i.e., http://localhost:8084/restaurant/customer/1 and we get the following output −

[
   {
      "id": 1,
      "name": "Pandas",
      "city": "DC"
   },
   {
      "id": 3,
      "name": "Little Italy",
      "city": "DC"
   }
]

This means that both the calls were correctly routed to the respective services.

Predicates & Filters Request

We had used Path predicate in our above example. Here are a few other important predicates −

Predicate Description
Cookie predicate (input: name and regex) Compares the cookie with the name to the regexp
Header predicate (input: name and regex) Compares the header with the name to the regexp
Host predicate (input: name and regex) Compares the name of the incoming to the regexp
Weight Predicate (input: Group name and the weight) Weight Predicate (input: Group name and the weight)

Filters are used to add/remove data from the request before sending the data to the downstream service or before sending the response back to the client.

Following are a few important filters for adding metadata.

Filter Description
Add request header filter (input: header and the value) Add a header and the value before forwarding the request downstream.
Add response header filter (input: header and the value) Add a header and the value before forwarding the request upstream that is to the client.
Redirect filter (input: status and URL) Adds a redirect header along with the URL before passing over o the downstream host.
ReWritePath (input: regexp and replacement) This is responsible for rewriting the path by replacing the regexp matched string with the input replacement.

The exhaustive list for filters and predicates is present at https://cloud.spring.io/spring-cloudgateway/reference/html/#the-rewritepath-gatewayfilter-factory

Advertisements