springboot使用默认资源映射
时间: 2024-01-30 09:04:08 浏览: 24
在Spring Boot中,可以使用默认的静态资源映射,只需要将静态资源文件放置在src/main/resources/static目录下即可。
例如,如果在static目录下有一个名为“test.png”的图片文件,那么可以通过访问“/test.png”来获取该文件。
另外,如果需要更改默认的静态资源路径,可以在application.properties文件中设置spring.resources.static-locations属性,例如:
```
spring.resources.static-locations=classpath:/static/,classpath:/public/
```
这样就会将静态资源的查找路径扩展到了classpath:/public/目录下。
相关问题
springboot静态资源映射
在Spring Boot中,静态资源映射是由Spring Boot默认提供的配置完成的。Spring Boot将所有访问映射到以下目录:classpath:/static、classpath:/public、classpath:/resources和classpath:/META-INF/resources。如果这些目录中有多张同名的图片,访问地址的优先级顺序为:META-INF/resources > resources > static > public。\[2\]
如果我们请求一个以".html"结尾的静态资源,Spring Boot会按照优先级高的文件夹先查找,然后再查找优先级低的文件夹,直到找到指定的静态资源为止。\[3\]
例如,在src/main/resources/static目录下创建了一个hello.html文件,当我们启动Spring Boot并访问"http://localhost:8080/hello.html"时,就会显示该hello.html页面。\[3\]
总结来说,Spring Boot对静态资源映射提供了默认配置,可以直接将静态资源放置在指定的目录中,无需进行额外的配置即可访问这些静态资源。\[1\]
#### 引用[.reference_title]
- *1* *3* [Spring Boot静态资源映射](https://blog.csdn.net/weixin_64842782/article/details/125089662)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [springboot静态资源映射](https://blog.csdn.net/ChOLg/article/details/102800978)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
Springboot对静态资源的映射规则
在Spring Boot中,静态资源默认存放在classpath下的“/static”、“/public”、“/resources”、“/META-INF/resources”目录下,Spring Boot会自动为这些目录下的静态资源创建一个URL映射。具体的URL映射规则如下:
- /webjars/**:用于访问WebJars资源(例如jquery、bootstrap等)
- /**:用于访问其他静态资源,例如图片、CSS、JavaScript等
例如,在项目的“src/main/resources/static”目录下有一个“test.css”文件,可以通过访问“http://localhost:8080/test.css”来访问这个文件。如果要修改默认的静态资源目录,可以在application.properties文件中添加以下配置:
```
# 修改静态资源目录
spring.resources.static-locations=classpath:/static/,classpath:/public/
```
这样就将默认的静态资源目录“/resources”、“/META-INF/resources”排除在外了,并且添加了两个新的静态资源目录“/static”、“/public”。这些新的目录同样会被Spring Boot自动创建URL映射。