Spring Webflux,如何转发到index.html以提供静态内容 [英] Spring Webflux, How to forward to index.html to serve static content

查看:225
本文介绍了Spring Webflux,如何转发到index.html以提供静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

spring-boot-starter-webflux(Spring Boot v2.0.0.M2)已经像在spring-boot-starter-web中一样配置为在资源中的静态文件夹中提供静态内容.但是它不会转发到index.html.在Spring MVC中,可以这样配置:

spring-boot-starter-webflux (Spring Boot v2.0.0.M2) is already configured like in a spring-boot-starter-web to serve static content in the static folder in resources. But it doesn't forward to index.html. In Spring MVC it is possible to configure like this:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("forward:/index.html");
}

如何在Spring Webflux中做到这一点?

How to do it in Spring Webflux?

推荐答案

在WebFilter中完成

Do it in WebFilter:

@Component
public class CustomWebFilter implements WebFilter {
  @Override
  public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    if (exchange.getRequest().getURI().getPath().equals("/")) {
        return chain.filter(exchange.mutate().request(exchange.getRequest().mutate().path("/index.html").build()).build());
    }

    return chain.filter(exchange);
  }
}

这篇关于Spring Webflux,如何转发到index.html以提供静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆