
- Spring Boot - Home
- Spring Boot - Introduction
- Spring Boot - Quick Start
- Spring Boot - Bootstrapping
- Spring Tool Suite
- Spring Boot - Tomcat Deployment
- Spring Boot - Build Systems
- Spring Boot - Code Structure
- Spring Beans & Dependency Injection
- Spring Boot - Runners
- Spring Boot - Starters
- Spring Boot - Application Properties
- Spring Boot - Configuration
- Spring Boot - Annotations
- Spring Boot - Logging
- Building RESTful Web Services
- Spring Boot - Exception Handling
- Spring Boot - Interceptor
- Spring Boot - Servlet Filter
- Spring Boot - Tomcat Port Number
- Spring Boot - Rest Template
- Spring Boot - File Handling
- Spring Boot - Service Components
- Spring Boot - Thymeleaf
- Consuming RESTful Web Services
- Spring Boot - CORS Support
- Spring Boot - Internationalization
- Spring Boot - Scheduling
- Spring Boot - Enabling HTTPS
- Spring Boot - Eureka Server
- Service Registration with Eureka
- Gateway Proxy Server and Routing
- Spring Cloud Configuration Server
- Spring Cloud Configuration Client
- Spring Boot - Actuator
- Spring Boot - Admin Server
- Spring Boot - Admin Client
- Spring Boot - Enabling Swagger2
- Spring Boot - Using SpringDoc OpenAPI
- Spring Boot - Creating Docker Image
- Tracing Micro Service Logs
- Spring Boot - Flyway Database
- Spring Boot - Sending Email
- Spring Boot - Hystrix
- Spring Boot - Web Socket
- Spring Boot - Batch Service
- Spring Boot - Apache Kafka
- Spring Boot - Twilio
- Spring Boot - Unit Test Cases
- Rest Controller Unit Test
- Spring Boot - Database Handling
- Securing Web Applications
- Spring Boot - OAuth2 with JWT
- Spring Boot - Google Cloud Platform
- Spring Boot - Google OAuth2 Sign-In
Spring Boot - Gateway Proxy Server and Routing
Gateway Proxy Server is a gateway application that handles all the requests and does the dynamic routing of microservice applications.
For Example, /api/user is mapped to the user service and /api/products is mapped to the product service and Zuul Server dynamically routes the requests to the respective backend application.
spring-cloud-starter-zuul dependency was used to integrate Zuul gateway server in spring boot project. Now it is deprecated and it replaced with spring-cloud-starter-gateway-mvc dependency. We'll using Gateway Server terminology instead of Zuul Server now onwards.
In this chapter, we are going to see in detail how to create Gateway Server application in Spring Boot.
Creating Gateway Server Application
The Gateway Server is bundled with Spring Cloud dependency. You can download the Spring Boot project from Spring Initializer page https://start.spring.io/ and choose the Zuul Server dependency.

Add the Routing Information in your main Spring Boot application. Here we're rerouting each request received to a different server while adding a sample request header.
GatewayApplication
package com.tutorialspoint.gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; @SpringBootApplication public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } @Bean public RouteLocator myRoutes(RouteLocatorBuilder builder) { return builder.routes() .route(p -> p .path("/**") .filters(f -> f.addRequestHeader("Src", "Tutorialspoint")) .uri("http://localhost:8080/")) .build(); } }
You will have to add the Spring Cloud Starter Gateway dependency in our build configuration file.
Maven users will have to add the following dependency in your pom.xml file −
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
Update application.properties to point to 8111 port
application.properties
spring.application.name=gateway server.port = 8111
For Gradle users, add the below dependency in your build.gradle file
compile('org.springframework.cloud:spring-cloud-starter-gateway')
application.yaml
spring application name: gateway server port: 8111
Note − The http://localhost:8080/ application should already be running before routing via Gateway Proxy. Refer to Consuming RESTful Web Services Chapter for REST Service details.
The complete build configuration file is given below.
Maven users can use the pom.xml file given below −
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.tutorialspoint</groupId> <artifactId>gateway</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gateway</name> <description>Demo project for Spring Boot</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>21</java.version> <spring-cloud.version>2023.0.3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Gradle users can use the build.gradle file given below −
buildscript { ext { springBootVersion = '3.3.3' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' group = 'com.tutorialspoint' version = '0.0.1-SNAPSHOT' sourceCompatibility = 21 repositories { mavenCentral() } ext { springCloudVersion = 'Edgware.RELEASE' } dependencies { compile('org.springframework.cloud:spring-cloud-starter-gateway') testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
You can create an executable JAR file, and run the Spring Boot application by using the Maven or Gradle commands given below −
For Maven, you can use the command given below −
mvn clean install
After BUILD SUCCESS, you can find the JAR file under the target directory.
For Gradle, you can use the command given below −
gradle clean build
After BUILD SUCCESSFUL, you can find the JAR file under the build/libs directory.
Now, run the JAR file by using the command shown below −
java jar <JARFILE>
You can find the application has started on the Tomcat port 8111 as shown here.
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ [32m :: Spring Boot :: [39m [2m (v3.3.3)[0;39m [2m2024-09-11T14:37:33.937+05:30[0;39m [32m INFO[0;39m [35m14996[0;39m [2m---[0;39m [2m[gateway] [ main][0;39m [2m[0;39m[36mc.t.gateway.GatewayApplication [0;39m [2m:[0;39m Starting GatewayApplication using Java 21.0.3 with PID 14996 (E:\Dev\gateway\target\classes started by Tutorialspoint in E:\Dev\gateway) [2m2024-09-11T14:37:33.940+05:30[0;39m [32m INFO[0;39m [35m14996[0;39m [2m---[0;39m [2m[gateway] [ main][0;39m [2m[0;39m[36mc.t.gateway.GatewayApplication [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default" ... [2m[0;39m[36mo.s.c.g.r.RouteDefinitionRouteLocator [0;39m [2m:[0;39m Loaded RoutePredicateFactory [CloudFoundryRouteService] [2m2024-09-11T14:37:35.796+05:30[0;39m [32m INFO[0;39m [35m14996[0;39m [2m---[0;39m [2m[gateway] [ main][0;39m [2m[0;39m[36mo.s.b.web.embedded.netty.NettyWebServer [0;39m [2m:[0;39m Netty started on port 8111 (http) [2m2024-09-11T14:37:35.832+05:30[0;39m [32m INFO[0;39m [35m14996[0;39m [2m---[0;39m [2m[gateway] [ main][0;39m [2m[0;39m[36mc.t.gateway.GatewayApplication [0;39m [2m:[0;39m Started GatewayApplication in 2.366 seconds (process running for 3.198)
Now, hit the URL http://localhost:8111/products in your web browser and you can see the output of http://localhost:8080/products REST Endpoint as shown below −
