Spring Boot静态资源路径的配置与修改详解
作者:杼蛘 时间:2022-09-15 19:27:08
默认路径
在Spring Boot 2.7.2版本中,查看默认静态资源路径,在WebProperties.class
中如下
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
可以看到默认资源路径有4个。
使用Spring Initializr
新建Spring Boot项目,自带static
目录,直接将前端资源文件放到该目录下,启动项目,访问http://localhost:端口号/资源目录/名称.html
即可;
例如,有一个front
目录,该目录下存在一个index.html
文件,将此目录放于src/main/resources/static
下,并且未修改端口号,访问http://localhost:8080/front/index.html
即可看到访问成功。
修改路径
使用配置文件进行修改
对于低版本,在配置文件application.yml
中如下:
spring:
resources:
static-locations: classpath:/
代表将资源目录直接放在src/main/resources/
下
但是,对于高版本,该方式已弃用,不推荐!!!
对于高版本,在配置文件application.yml
中如下:
spring:
web:
resources:
static-locations: classpath:/
高版本这样设置,可以成功访问http://localhost:8080/front/index.html
使用配置类进行修改
新建配置类WebMvcConfig.java
继承WebMvcConfigurationSupport
类
package com.aiw.waimai.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
/**
* 设置静态资源映射
* @param registry
*/
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
log.info("开始进行静态资源映射。。。");
registry.addResourceHandler("/**").addResourceLocations("classpath:/");
}
}
可以成功访问http://localhost:8080/front/index.html
注意:两种配置方式不可同时存在,并且修改后默认的访问路径就失效了;对于配置类方式,@Slf4j是Lombok提供的注解,方便打印日志,非必须
【更新】网上看到WebMvcConfigurationSupport
已过时,故更新为实现WebMvcConfigurer
接口
package com.aiw.waimai.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
/**
* 设置静态资源映射
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
log.info("开始进行静态资源映射。。。");
registry.addResourceHandler("/**").addResourceLocations("classpath:/");
}
}
来源:https://blog.csdn.net/qq_45917176/article/details/126300050
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android中外接键盘的检测的实现
JAVA设计模式----建造者模式详解
![](https://img.aspxhome.com/file/2023/6/65316_0s.png)
使用JavaWeb webSocket实现简易的点对点聊天功能实例代码
SSH原理及两种登录方法图文详解
![](https://img.aspxhome.com/file/2023/8/59038_0s.png)
springboot+vue部署按照及运行方法
详解SpringBoot注解读取配置文件的方式
解决mybatis-plus自动配置的mapper.xml与java接口映射问题
![](https://img.aspxhome.com/file/2023/5/58045_0s.gif)
java数据结构之希尔排序
详解JAVA中static的作用
![](https://img.aspxhome.com/file/2023/2/61262_0s.jpg)
c#与WMI使用技巧集第1/2页
深入解析Java的Spring框架中bean的依赖注入
![](https://img.aspxhome.com/file/2023/8/66188_0s.png)
Java设计模式之工厂模式
![](https://img.aspxhome.com/file/2023/9/60339_0s.png)
Java的Swing编程中使用SwingWorker线程模式及顶层容器
Druid基本配置及内置监控使用_动力节点Java学院整理
Flutter基于Dart Unwrapping Multiple Optional小技巧
MyBatis动态SQL标签用法实例详解
使用Logback日志保存到相对路径的操作
Idea配置Maven阿里云镜像加速的实现
![](https://img.aspxhome.com/file/2023/2/60682_0s.png)