java - Spring Webflux,如何转发到 index.html 以提供静态内容

标签 java spring spring-mvc spring-boot spring-webflux

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

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

在Spring Webflux中怎么做?

最佳答案

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

关于java - Spring Webflux,如何转发到 index.html 以提供静态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45147280/

相关文章:

spring - Autowiring 的 sessionFactory 对象为空

java - ORA-28040 : No matching authentication protocol exception when using groovy. 带有 oracle 12c 的 sql 包

java - 线程中的异常 "main"递归中的 java.lang.StackOverflowError

java - Spring-Security : SecurityContextHolder not populated with anonymous token, 因为它已经包含

java - Jersey 2 多部分/表单数据问题。输入流为空(可用=0)

java - 配置 Spring Boot Test 以使用单独的 bean 集

java - 使用 FileCopyUtils.copy 在 Spring 上传大文件

java - NotReadablePropertyException : Invalid property 'id' of bean class [java. util.LinkedHashMap]

java - Mockito:如何模拟 setter 的 setter?

javascript - 成功 import-jdl 后,JHipster 实体菜单为空