Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How SpringBoot overrides the path of addResourceHandlers mapping files

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report