Spring Webflux, How to forward to index.html to serve static content Spring Webflux, How to forward to index.html to serve static content spring spring

Spring Webflux, How to forward to index.html to serve static content


Do it in WebFilter:

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


import static org.springframework.web.reactive.function.server.RequestPredicates.GET;import static org.springframework.web.reactive.function.server.RouterFunctions.route;import static org.springframework.web.reactive.function.server.ServerResponse.ok;@Beanpublic RouterFunction<ServerResponse> indexRouter(@Value("classpath:/static/index.html") final Resource indexHtml) {return route(GET("/"), request -> ok().contentType(MediaType.TEXT_HTML).bodyValue(indexHtml));}