In web application development, the processing of static resources (such as CSS, JavaScript, pictures, etc.) is a basic but important link.
As a powerful application framework, SpringBoot provides developers with a variety of ways to handle static resources flexibly.
This article will introduce 7 static resource processing techniques in SpringBoot.
1. Utilize the default static resource location
SpringBoot provides four static resource directories by default:
/static
/public
/resources
/META-INF/resources
These directories are all located under the classpath and are given priority in the order mentioned above. Put resources into these directories and you can usehttp://localhost:8080/Resource name
Direct access.
// Example: Resources placed in /static/css/// Can be accessed via the following URL// http://localhost:8080/css/
2. Customize the static resource location
You can modify the default static resource location through configuration:
spring: web: resources: static-locations: - classpath:/custom/ - classpath:/static/ - file:/path/to/external/resources/
This allows you to add custom directories, or even absolute paths on the server.
3. Use WebJars to manage front-end dependencies
WebJars provides a way to manage front-end resources through Maven or Gradle, making them as manageable as Java dependencies.
<!-- Add dependencies to --> <dependency> <groupId></groupId> <artifactId>jquery</artifactId> <version>3.6.0</version> </dependency>
Then you can access it like this:
<script src="/webjars/jquery/3.6.0/"></script>
4. Resource version control
SpringBoot supports version control of static resources, which can solve browser caching problems:
spring: web: resources: chain: strategy: content: enabled: true paths: /**
In this way, SpringBoot will generate fingerprints based on content, such as:
<link href="/css/" rel="stylesheet">
5. Configure static resource cache
Cache control can be configured for static resources:
spring: web: resources: cache: period: 3600 cachecontrol: max-age: 3600 must-revalidate: true
This will add appropriate HTTP cache headers to static resources, improving performance.
6. Configure the static resource access path prefix
You can add a unified access prefix to all static resources:
spring: mvc: static-path-pattern: /resources/**
After configuration, all static resources need to be passed/resources/
Prefix access:
http://localhost:8080/resources/css/
This helps distinguish APIs from static resources and is easy to manage.
7. Use ResourceHandlers to customize resource processing
For more complex requirements, it can be achieved byWebMvcConfigurer
Interface custom resource processor:
@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // Map /files/** to file:/var/www/files/ ("/files/**") .addResourceLocations("file:/var/www/files/") .setCachePeriod(3600) .resourceChain(true) .addResolver(new VersionResourceResolver() .addContentVersionStrategy("/**")); // Specific version of resource processing ("/js/**") .addResourceLocations("classpath:/static/js/") .resourceChain(true) .addResolver(new VersionResourceResolver() .addFixedVersionStrategy("v1", "/**")); } }
This approach provides maximum flexibility and allows different processing strategies to be configured for different paths.
Summarize
SpringBoot provides rich features and flexible configuration options for static resource processing. You can choose to use default configuration, simple customization or advanced configuration methods according to project needs.
By rationally using these techniques, you can better manage static resources in SpringBoot projects while ensuring the convenience of development.
This is the end of this article about 7 tips for static resource processing in SpringBoot. For more related content on static resource processing in SpringBoot, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!