一灰灰blog 一灰灰blog
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)

一灰灰blog

资深搬运工
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)
  • 基础系列

  • DB系列

  • 搜索系列

  • MQ系列

  • WEB系列

    • Request

    • Response

    • RestTemplate

    • WebClient

    • WebFlux

      • 【WEB系列】WebFlux之初体验
      • 【WEB系列】WebFlux静态资源配置与访问
        • I. 默认配置
          • 1. 项目演示
          • 2. Url映射
        • II. 自定义配置路径
          • 1. 配置修改
          • 2. WebFluxConfigurer添加映射
          • 3. @Value方式
        • III. 小结
        • II. 其他
          • 0. 项目
          • 1. 一灰灰Blog
      • 【WBE系列】WebFlux之Path参数解析与url映射
      • 【WEB系列】WebFlux之header参数解析
    • WebSocket

    • Web三剑客

    • 实例

    • 其他

  • 中间件

  • 运维

  • SpringSecurity

  • SpringCloud

  • Spring系列
  • WEB系列
  • WebFlux
一灰灰
2020-06-12

【WEB系列】WebFlux静态资源配置与访问

上一篇博文介绍SpringMVC的静态资源访问,那么在WebFlux中,静态资源的访问姿势是否一致呢

# I. 默认配置

与SpringBoot的默认配置一样,WebFlux同样是classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

即,将静态文件放在这四个目录下,可以直接访问

# 1. 项目演示

创建一个SpringBoot项目,添加依赖(本文使用的版本为: 2.2.1-RELEASE)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
1
2
3
4

在资源路径下添加目录 static,目录下添加两个html文件,如下图

实现启动类,不添加额外逻辑,既可以直接通过完整url方式访问静态资源

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
1
2
3
4
5
6

主要观察上面三个请求,放在index.html是无法直接访问到的,因为它所在的目录并不在默认的四个静态资源路径中

# 2. Url映射

上面是直接通过静态资源文件名的方式进行访问,那么WebFlux是否可以实现SpringMVC那种,根据视图名返回View的方式呢?

@Controller
public class ViewAction {
    @GetMapping(path = "a")
    public String a() {
        return "a.html";
    }
}
1
2
3
4
5
6
7

直接访问,结果发现500,找不到名为a.html的视图

这种方式不行的话,改用WebFlux的路由写法

@Bean
public RouterFunction<ServerResponse> indexRouter() {
    return RouterFunctions.route(RequestPredicates.GET("/b"),
                    request -> ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue("b.html");
}
1
2
3
4
5

# II. 自定义配置路径

如果我们希望指定一个自定义的路径,是否可以如SpringMvc那样,修改配置or代码设置映射完成呢?

在资源目录下,新加两个文件夹,分别是 o1, o2

# 1. 配置修改

如SpringMVC,修改静态资源配置

spring:
  resources:
    static-locations: classpath:/o1/,classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
1
2
3

然后访问 /o1.html,发现404,这种直接修改配置方式不行!!!

# 2. WebFluxConfigurer添加映射

参考自官方文档: web-reactive.html#webflux-config-static-resources (opens new window)

直接修改启动类,实现WebFluxConfigurer接口,手动添加资源映射

@SpringBootApplication
public class Application implements WebFluxConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/o2/");
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

接着访问 /o2.html

# 3. @Value方式

除了上述手动映射的方式之外,还有一种非主流的是方式,如

@Bean
public RouterFunction<ServerResponse> indexRouter(@Value("classpath:/index.html") final Resource indexHtml,
        @Value("classpath:/self/s.html") final Resource sHtml) {
    return RouterFunctions.route(RequestPredicates.GET("/index"),
            request -> ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(indexHtml))
            .andRoute(RequestPredicates.GET("/s"),
                    request -> ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(sHtml));
}
1
2
3
4
5
6
7
8

请注意上面的两个文件, s.html, index.html都不在默认的静态资源目录下

# III. 小结

文中给出了WebFlux的静态资源访问姿势,与SpringMVC有一些区别

  • url映射时,直接返回视图名,会提示Could not resolve view with name xxx
  • 通过修改配置spring.resources.static-locations 指定新的静态资源目录无效

在WebFlux中,推荐使用实现WebFluxConfigure接口的方式,重写addResourceHandlers方法来自定义资源路径映射

也可以针对单独的静态资源,借助@Value来手动路由

# II. 其他

# 0. 项目

  • 工程:https://github.com/liuyueyi/spring-boot-demo (opens new window)
  • 源码:https://github.com/liuyueyi/spring-boot-demo/blob/master/spring-boot/200-webflux (opens new window)

# 1. 一灰灰Blog

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

  • 一灰灰Blog个人博客 https://blog.hhui.top (opens new window)
  • 一灰灰Blog-Spring专题博客 http://spring.hhui.top (opens new window)

一灰灰blog

编辑 (opens new window)
#WebFlux#静态资源
上次更新: 2021/10/15, 19:56:22
【WEB系列】WebFlux之初体验
【WBE系列】WebFlux之Path参数解析与url映射

← 【WEB系列】WebFlux之初体验 【WBE系列】WebFlux之Path参数解析与url映射→

最近更新
01
【基础系列】基于maven多环境配置
04-25
02
【WEB系列】内嵌Tomcat配置Accesslog日志文件生成位置源码探索
04-24
03
【搜索系列】ES查询常用实例演示
04-18
更多文章>
Theme by Vdoing | Copyright © 2017-2022 一灰灰Blog
MIT License | 鄂ICP备18017282号 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×