Serving Static Content with Spring Cloud Gateway

author-image   By Dhiraj,   10 August, 2019 0K

In this article, we will discuss the different ways to serve static content from a Spring Cloud Gateway. Any static content such as image, .html file, JS or CSS file can be served from default classpath location, or custom classpath location or from local disk location.

The best practice to host your images or other static contents is the CDN but we can also host static files at the Gateway level. Static files can be any image or .html file. As spring cloud gateway is developed upon spring web flux, we can use the spring web flux way to serve our static content from spring cloud gateway.

By default, Spring Boot serves static content from /public, /static, /resources, /META-INF/resources. Hence, without any extra configuration, you can place your static content in any one of these folders and it can be directly accessed with a HTTP request.

For example, below is a sample spring cloud gateway project structure that we created in our last tutorial of spring cloud gateway with an image placed in the /static folder.

spring-cloud-gateway-static-resource

This image can be directly accessed with the URL http://localhost:8088/images/aes.PNG.

spring-cloud-gateway-static-resource-access-browser

Serving Static Content from Classpath

It is also possible to serve static contents from any custom directory present at classpath. For example, I have a .html, CSS or JS file that I want to serve it from cloud gateway and I have placed it inside the /ui directory.

For this kind of scenarios, we need to define a RouterFunction Bean and configure the locations of these resources explicitly.

@Bean
RouterFunction staticResourceLocator(){
	return RouterFunctions.resources("/portal/**", new ClassPathResource("ui/"));
}

It is important to end resource path with slash (/) so it is considered a root for resources tree.

In this case, we can access a .html file placed inside /ui folder with url localhost:8088/portal/index.html

This can also be configured with application.properties configuration with spring.webflux.static-path-pattern

Serving Static Content from Local Disk

The problem with serving static content from Classpath is that we may require to redeploy our application if we have an update in the static resources and require some technical hands to deal with these scenarios.

Hence, we can also configure our gateway app to serve static content from local disk. In this case, we need to use FileSystemResource

@Bean
RouterFunction staticResourceLocator(){
	return RouterFunctions.resources("/portal/**", new FileSystemResource("D:\\website\\spring-cloud-static-resource/"));
}

Now, if we have a img.PNG located inside a folder D:\\website\\spring-cloud-static-resource, then that can be accessed with the URL localhost:8088/portal/img1.PNG

If You Appreciate This, You Can Consider:

  • Like us at: Facebook or follow us at Twitter
  • Share this article on social media or with your teammates.
  • We are thankful for your never ending support.

About The Author

author-image

I am an energetic professional who enjoys the challenges involved in working with people and resolving real-time problems. Technical expertise in building highly scalable, distributed and self-healing cloud applications. Technical Skills: Java/J2EE, Spring Framework, Hibernate, Angular, Reactive Programming, Microservices, Rest APIs, Kafka, ELK, etc.

Further Reading on Spring Cloud

1 Spring Boot Mongodb Crud

2 Spring Cloud Tutorial

3 Refresh Property Config Runtime

4 Spring Cloud Netflix Eureka

5 Introduction To Microservices

6 Encrypt Decrypt Cloud Config Properties

7 Spring Webflux Rest Authentication

8 Spring Cloud Feign Example