springcloud 如何配置静态资源处理
时间: 2024-11-23 10:50:10 浏览: 38
在Spring Cloud中,如果你想要正确地处理静态资源,可以使用Spring WebMvc或Spring Boot Actuator来配置。以下是基本步骤:
1. **使用Spring WebMvc**:
- 添加依赖:在pom.xml中添加`spring-boot-starter-web`或`spring-boot-starter-webmvc`。
- 配置Spring MVC的`ResourceHandlerRegistry`。在application.yml或application.properties中,添加类似下面的内容:
```yaml
spring:
mvc:
static-path-pattern: '/static/**'
resource-chain:
defaults:
# 使用Spring ResourceHttpRequestHandler处理静态资源
- path=/static/, uri=classpath:/META-INF/resources/
- path=/ favicon.ico, uri=/favicon.ico
```
2. **使用Spring Boot Actuator**:
- 启用Actuator并添加web服务端点。打开application.yml,添加`management.endpoints.web.exposure.include`属性,并包含`info`或自定义端点(例如`health`)。
- 默认情况下,Actuator会提供一个静态资源目录。但是,如果你想自定义静态资源,可以在`endpoints.web.pathMapping`里指定路径,然后指向你的资源目录。
记得重启应用让更改生效。如果静态资源仍然无法访问,检查一下网络配置、防火墙规则以及资源文件是否存在。
阅读全文
相关推荐


















