Wednesday, 24 March 2021

Spring boot: Serve static resources

In this post, I am going to explain how to serve static resources using spring boot application.

 

By implementing the interface ‘WebMvcConfigurer’, you can specify the location of static resources.

 

Example

@Configuration
public class StaticResourceConfiguration implements WebMvcConfigurer {

	private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
			"classpath:/resources/", "classpath:/static/", "classpath:/public/" };

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
	}
}

 

In the above example, I am specifying spring to look at the locations.

a.   /META-INF/resources/

b.   /resources

c.    /static

d.   /public

 

Find the below working application.

 

Step 1: Create new maven project ‘serve-static-content’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

 

<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>com.sample.app</groupId>
	<artifactId>serve-static-content</artifactId>
	<version>1</version>


	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.0</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

	</dependencies>
</project>

 

Step 3: Create folders static/html, static/images under src/main/resources folder.

 

Create hello.html file under static/html folder.

 

hello.html

 

<html>

	<head>
		<title>Hello World</title>
	</head>
	
	<body>
		<h1>Hello World</h1>
	</body>

</html>

 

Create banner.png file under static/images folder.



 

Step 4: Create a package ‘com.sample.app.config’ and define StaticResourceConfiguration class.

 

StaticResourceConfiguration.java

package com.sample.app.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class StaticResourceConfiguration implements WebMvcConfigurer {

	private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
			"classpath:/resources/", "classpath:/static/", "classpath:/public/" };

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
	}
}

 

Step 5: Define App class in the package ‘com.sample.app’.

 

App.java

 

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
	public static void main(String[] args) {

		SpringApplication.run(App.class, args);

	}

}

Total project structure looks like below.



Run App.java

 

Open the url ‘http://localhost:8080/html/hello.html’ in browser, you will see following screen.

 


  

Open the url ‘localhost:8080/images/banner.png’, you will see following screen.

 

You will see the banner image.

  


 

You can download the complete working application from below link.

https://github.com/harikrishna553/springboot/tree/master/rest/serve-static-content

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment