

Table of Contents
1. Overview
This article is about to Spring boot request routing example using zuul API. zuul API is used to route request which is specially use for micro service architecture, We can take zuul routing advantages as bellow:
- Migration of old service to new service
- Redirect specific request to another domain or submain for manage load
- Redirect request to specific services means single point of entry in application.
- i.e request start with
/account
redirectaccount
service - i.e request start with
/admin
redirect toadmin
service. - Syntax : zuul.routers.[SERVICE_NAE]:[URL_PATTERN]
- In bellow example all request
/users/*
redirect to user service.
- i.e request start with
zuul: routes: users: /users/**
2. Example

Spring boot request routing example
2.1 pom.xml
Zuul API require of group org.springframework.cloud
and artifact Id spring-cloud-starter-netflix-zuul
in classpath.
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring-boot-example</groupId> <artifactId>Spring-boot-request-routing-example</artifactId> <version>1.0-SNAPSHOT</version> <description>Spring boot request routing example</description> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-zuul --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> <!-- zuul api has been used for spring boot request routing --> <version>1.4.0.RELEASE</version> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.2 application.yml
/api-1.0/**
redirect to /api-2.0/**
means that /api-1.0/demo
to /api-2.0/demo
for more details refer spring cloud netflix
zuul: routes: first: path: /api-1.0/** url: forward:/api-2.0 # /api-1.0/** redirect to /api-2.0/** means that /api-1.0/demo to /api-2.0/demo
2.3 SpringBootConfig
@EnableZuulProxy
annotation will enable zuul configuration.
package com.javadeveloperzone; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.context.annotation.ComponentScan; /** * Created by JavaDeveloperZone on 19-07-2017. */ @SpringBootApplication @ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute @EnableZuulProxy // to enable zuul request routing public class SpringBootConfig { public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootConfig.class, args); // it wil start application } }
2.4 ZuulAPIController
This controller will full-fill both request new API as well as old API means that /api-1.0/demo
and /api-2.0/demo
will be handle here:
package com.javadeveloperzone.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by javadeveloperzone on 15-12-2017. */ @RestController @RequestMapping("/api-2.0") public class ZuulAPIController { @RequestMapping("/demo") public String getData(){ return "Response from API-2.0"; } }
2.5 Output:
http://localhost:8080/api-1.0/demo
is pointing (routing) to http://localhost:8080/api-2.0/demo

Spring boot request routing example output
Let Check Endpoints
1./routes
endpoint will show information about configure routers in zuul:
http://localhost:8080/routes
{"/api-1.0/**":"forward:/api-2.0"}
2./routes?format=details
will display more specific information about routing in zuul:
http://localhost:8080/routes?format=details
{"/api-1.0/**":{"id":"first","fullPath":"/api-1.0/**","location":"forward:/api-2.0","path":"/**","prefix":"/api-1.0","retryable":false,"customSensitiveHeaders":false,"prefixStripped":true}}
3. References