SpringBoot重写addResourceHandlers映射文件路径方式
作者:维1 时间:2022-05-26 19:02:40
重写addResourceHandlers映射文件路径
在看一个博客源码发现页面的图片所映射的地址在SpringBoot静态资源文件夹下找不到原來在这里是通过下面这段代码,将/store/**地址映射为getStorePath()得到的地址
在此记录以下
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);
}
配置本地资源映射路径 addResourceHandlers
实现 WebMvcConfigurer,重写addResourceHandlers(ResourceHandlerRegistry registry)方法
addResourceHandler()
:添加的是访问路径addResourceLocations()
:添加的是映射后的真实路径,映射的真实路径末尾必须加 / ,不然映射不到,这个问题困扰了我半天, / 适用于 windows和linux
如下:
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;
/**
* * 配置
*/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
/**
* @author: JiaXinMa
* @description: 访问静态文件
* @date: 2021/4/15
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//访问路径
registry.addResourceHandler("/api/upload/**")
//映射真实路径
.addResourceLocations("file:" + System.getProperty("user.dir") + "/");//必须加"/",不然映射不到
}
}
System.getProperty(“user.dir”) 是当前项目路径
成功访问如下
来源:https://blog.csdn.net/qq_37859539/article/details/82912851
标签:SpringBoot,addResourceHandlers,映射,路径
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mybatis根据表逆向自动化生成代码的实现
2023-03-12 15:39:02
![](https://img.aspxhome.com/file/2023/9/61079_0s.png)
使用Jenkins来构建GIT+Maven项目的方法步骤
2021-11-15 07:57:33
![](https://img.aspxhome.com/file/2023/8/61838_0s.png)
Android自定义Banner轮播效果
2023-08-05 23:34:06
Spring整合MyBatis图示过程解析
2023-11-13 11:45:09
![](https://img.aspxhome.com/file/2023/8/59188_0s.png)
Java中为什么start方法不能重复调用而run方法可以?
2023-11-15 03:04:02
![](https://img.aspxhome.com/file/2023/8/59598_0s.webp)
java.lang.Runtime.exec的左膀右臂:流输入和流读取详解
2023-08-06 04:59:03
![](https://img.aspxhome.com/file/2023/4/57804_0s.jpg)
简单实现Java web服务器
2023-10-11 16:47:13
![](https://img.aspxhome.com/file/2023/6/64536_0s.jpg)
java使用计算md5校验码方式比较两个文件是否相同
2023-05-10 23:43:16
教你如何使用Java输出各种形状
2023-08-21 00:46:55
Java 数据结构与算法系列精讲之KMP算法
2023-05-06 14:55:55
![](https://img.aspxhome.com/file/2023/7/64907_0s.jpg)
Android 消息机制问题总结
2023-08-06 03:10:49
![](https://img.aspxhome.com/file/2023/5/84715_0s.jpg)
关于@GetMapping和@GetMapping(value=““)的区别
2023-11-27 11:07:37
![](https://img.aspxhome.com/file/2023/2/59992_0s.png)
Java实现TopK问题的方法
2023-11-10 20:32:14
OpenCV实现直线检测并消除
2023-07-12 20:44:36
![](https://img.aspxhome.com/file/2023/3/92493_0s.jpg)
java String 转成Double二维数组的方法
2023-04-28 12:11:02
Java泛型机制与反射原理相关知识总结
2023-11-11 06:02:15
![](https://img.aspxhome.com/file/2023/4/58914_0s.jpg)
mybatis中使用oracle关键字出错的解决方法
2022-05-14 10:19:23
Android 程序执行Linux命令的解决方法及注意事项
2023-07-13 00:14:58
![](https://img.aspxhome.com/file/2023/4/83524_0s.jpg)
基于Java利用static实现单例模式
2021-12-20 06:07:32
详解Maven profile配置管理及激活profile的几种方式
2022-07-01 08:54:46