composer 完整路径才能访问_【SpringBoot WEB系列】WebFlux静态资源配置与访问

v2-6cb0ec20c7745f50c1540662b7acd43c_1440w.jpg?source=172ae18b
【SpringBoot 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>

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

v2-30ff983f9ba302c827a91d3816f13c8c_b.jpg

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

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

v2-f7cfe3e3ef6d8b5e4e9d9041a37c1363_b.jpg

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

2. Url映射

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

@Controller
public class ViewAction {
    @GetMapping(path = "a")
    public String a() {
        return "a.html";
    }
}

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

v2-4a5f584b599164aad3bc4875f6600537_b.jpg

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

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

v2-9c7787ec44df2752e742c64eda463bda_b.jpg

II. 自定义配置路径

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

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

v2-8681bfe76edcc991ad02b61d64148c38_b.jpg

1. 配置修改

如SpringMVC,修改静态资源配置

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

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

v2-cbe0c2773bda3d7b493c23fc29c50429_b.jpg

2. WebFluxConfigurer添加映射

参考自官方文档: web-reactive.html#webflux-config-static-resources

直接修改启动类,实现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);
    }
}

接着访问 /o2.html

v2-7f5239012a0ca05e658550e0b1c2fa8a_b.jpg

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));
}

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

v2-c615a54bb4dcee0e9ec076d1f356b64f_b.jpg

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
  • 源码:https://github.com/liuyueyi/spring-boot-demo/blob/master/spring-boot/200-webflux

1. 一灰灰Blog

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

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

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

v2-b544d232416504fde2d7571b29aaebb8_b.jpg
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值