Serving Static Content with Spring Cloud Gateway

Serving Static Content with Spring Cloud Gateway thumbnail
32K
By Dhiraj 10 August, 2019

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

Share

If You Appreciate This, You Can Consider:

We are thankful for your never ending support.

About The Author

author-image
A technology savvy professional with an exceptional capacity to analyze, solve problems and multi-task. Technical expertise in highly scalable distributed systems, self-healing systems, and service-oriented architecture. Technical Skills: Java/J2EE, Spring, Hibernate, Reactive Programming, Microservices, Hystrix, Rest APIs, Java 8, Kafka, Kibana, Elasticsearch, etc.

Further Reading on Spring Cloud