How to Configure Spring Cloud Gateway Route Predicates for Precise Routing

This guide explains how to configure various Spring Cloud Gateway route predicates—including After, Before, Between, Cookie, Header, Host, Method, Path, and Query—providing YAML examples, execution flow, and test results to achieve precise request routing in a Spring Boot microservice environment.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Configure Spring Cloud Gateway Route Predicates for Precise Routing

Environment: springboot2.3.7 + spring cloud Hoxton.SR9

Spring Cloud Gateway workflow: the client calls the gateway, the request is matched to a route based on configured predicates, then passes through pre‑filters, the proxy request is sent, and post‑filters run after the response.

After Route Predicate Purpose: matches requests that occur after a specified date‑time (java ZonedDateTime).

spring:
  cloud:
    gateway:
      enabled: true
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true
      routes:
        - id: after_route
          uri: https://www.baidu.com
          predicates:
            - After=2021-01-10T17:42:47.789-07:00

Only requests after 2021‑01‑10T17:42:47.789‑07:00 are allowed; earlier requests receive 404.

Before Route Predicate Purpose: matches requests that occur before a specified date‑time.

spring:
  cloud:
    gateway:
      enabled: true
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true
      routes:
        - id: before_route
          uri: https://www.qq.com
          predicates:
            - Before=2021-01-10T17:42:47.789-07:00

Requests before the given time are routed; later requests return 404.

Between Route Predicate Purpose: matches requests whose date‑time falls between two specified ZonedDateTime values.

spring:
  cloud:
    gateway:
      enabled: true
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true
      routes:
        - id: between_route
          uri: https://www.163.com
          predicates:
            - Between=2021-01-01T17:42:47,2021-01-10T17:42:47

Requests outside this range receive 404.

Cookie Route Predicate Purpose: matches only when the request contains a specific cookie (name=value).

spring:
  cloud:
    gateway:
      enabled: true
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true
      routes:
        - id: cookie_route
          uri: https://www.baidu.com
          predicates:
            - Cookie=code,testiptv255

If the request header includes a cookie named code with value testiptv255 , the route matches; otherwise 404.

Header Route Predicate Purpose: matches when a specific request header is present with a given value.

spring:
  cloud:
    gateway:
      enabled: true
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true
      routes:
        - id: header_route
          uri: https://www.baidu.com
          predicates:
            - Header=Host,localhost:20000

Only requests whose Host header equals localhost:20000 are routed.

Host Route Predicate Purpose: matches when the request’s Host header matches any pattern in the list.

spring:
  cloud:
    gateway:
      enabled: true
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true
      routes:
        - id: host_route
          uri: https://www.baidu.com
          predicates:
            - Host=**.xg.com,localhost:20000

Requests with host www.xg.com , xxx.xg.com , or localhost:20000 are routed.

Method Route Predicate Purpose: matches when the HTTP method of the request is one of the specified methods.

spring:
  cloud:
    gateway:
      enabled: true
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true
      routes:
        - id: method_route
          uri: https://www.baidu.com
          predicates:
            - Method=GET,POST

GET or POST requests are routed; other methods receive 404.

Path Route Predicate Purpose: matches when the request path matches one of the specified patterns.

spring:
  cloud:
    gateway:
      enabled: true
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true
      routes:
        - id: path_route
          uri: https://www.baidu.com
          predicates:
            - Path=/api-a/{segment},/api-1/{segment}

Requests to /api-a/… or /api-1/… are routed; other paths return 404.

Query Route Predicate Purpose: matches when the request contains a specific query parameter (optionally with a specific value).

spring:
  cloud:
    gateway:
      enabled: true
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true
      routes:
        - id: query_route
          uri: https://www.baidu.com
          predicates:
            - Query=name
        - id: query_route
          uri: https://www.baidu.com
          predicates:
            - Query=name,admin

Mode 1 matches any request with a name parameter; Mode 2 matches only when name=admin . Non‑matching queries result in 404.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

MicroservicesSpring Cloud Gatewayroute predicates
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.