1. Identify problems
In a project, we may need to store static resources in the SpringBoot project. We usually store static resources in the resources/static directory, which can be accessed through http://localhost:8080/. Has been introduced the static jar of resources, but we want to modify the static resources content, such as change is js config file configuration file, you need to modify the interface request address, secret key information, such as static resource will be in the current path to create a same file, the static resources, introduced to cover, found no effect, It still reads files in the imported JAR and does not read static resources in the current module
2. Treatment methods
In the resourceProperties.calss source code, we can see that static resources read more than one path by default
The priorities are as follows: 1 -> 2 -> 3 -> 4
- Priority 1: classpath:/ meta-INF /resources/
- Priority 2: classpath:/resources/
- Priority 3: classpath:/static/
- Priority 4: classpath:/public/
If you override the addResourceHandlers method under WebMvcConfigurer
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(new String[]{"/ * *"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/"."classpath:/resources/"."classpath:/static/"."classpath:/public/"});
}
Copy the code
3. Processing results
Jar static resources are usually in the static directory. We can overwrite the original file by creating the same file in the same directory of resources. Isn’t it nice?
-
Personal open source project (general background management system) – > https://gitee.com/wslxm/spring-boot-plus2, like can have a look
-
The end of this article, if you feel useful, move a small hand to like or follow the chant, will continue to update more content from time to time… Thank you for watching!