Get the App
SLTechnology News&Howtos  ›  Development  › 

How SpringBoot overrides the path of addResourceHandlers mapping files

Shulou Source: shulou.com Published: 2022-06-01 12:46:26 10月28日 Update

This article mainly introduces "SpringBoot how to rewrite addResourceHandlers mapping file path" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "SpringBoot how to rewrite addResourceHandlers mapping file path" article can help you solve the problem.

Override the addResourceHandlers mapping file path

After looking at the source code of a blog, I found that the address mapped by the picture of the page cannot be found under the SpringBoot static resources folder. Here, the / store/** address is mapped to the address obtained by getStorePath () through the following code.

Record the following registry.addResourceHandler ("/ store/**") .addResourceLocations (getStorePath ()); @ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {registry.addResourceHandler ("/ dist/**") .addResourceLocations ("classpath:/static/dist/"); registry.addResourceHandler ("/ theme/**"). AddResourceLocations ("classpath:/static/theme/"); registry.addResourceHandler ("/ store/**") .addResourceLocations (getStorePath ()) Super.addResourceHandlers (registry);} configure the local resource mapping path addResourceHandlers

Implement WebMvcConfigurer, override the addResourceHandlers (ResourceHandlerRegistry registry) method

AddResourceHandler (): the access path is added

AddResourceLocations (): the real path after mapping is added, and / must be added at the end of the real path of the mapping, otherwise the mapping will not be achieved. This problem has bothered me for a long time, / applicable to windows and linux

As follows:

Package cn.mindgd.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer / * interceptor configuration * / @ Configurationpublic class InterceptorConfig implements WebMvcConfigurer {/ * * @ author: JiaXinMa * @ description: access static file * @ date: 2021-4-15 * / @ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {/ / access path registry.addResourceHandler ("/ api/upload/**") / / Mapping Real path .addResourceLocations ("file:" + System.getProperty ("user.dir") + "/") / / "/" must be added, otherwise it will not be mapped}}.

System.getProperty ("user.dir") is the current project path

The successful visit is as follows

This is the end of the introduction to "how SpringBoot rewrites the path of addResourceHandlers mapping files". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Path file address knowledge method industry resource problem static configuration different practical successful code content blog picture utility actual folder Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info macOS MariaDB Redmi Linux