0

Hi everyone i am searching now the full day and i do not found a solution. I could server static file in a mvc spring application without problems but with webflux i do not found a way how i can serve them.

I put in ressource a folder with the name static and in there its a simple html file.

My configuration looks like:

@Configuration
@EnableWebFlux
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class WebConfig implements WebFluxConfigurer {


    @Bean
    public RouterFunction<ServerResponse> route() {
        return RouterFunctions.resources("/", new ClassPathResource("static/"));
}

When i start the application and go to localhost i just received a 404 response.

I also try it with adding:

spring.webflux.static-path-pattern = /**
spring.web.resources.static-locations = classpath:/static/

to the application.properties but i still received the 404 not found.

Even when i added Thymeleaf to my dependencies i still get 404.

Hopefully someone knows what to do.

1 Answers1

4

What i think you are missing is basically to tell on what type (GET) of request you want to serve data.

Here is an old pice of code i found that i have used when i served a react application from a public folder in the resource folder.

When doing a GET against /* we fetch the index.html. If the index is containing javascript that does returning requests they are caught in the second router, serving whatever is in the public folder.

@Configuration
public class HtmlRoutes {

    @Bean
    public RouterFunction<ServerResponse> htmlRouter(@Value("classpath:/public/index.html") Resource html) {
        return route(GET("/*"), request -> ok()
                .contentType(MediaType.TEXT_HTML)
                .bodyValue(html)
        );
    }

    @Bean
    public RouterFunction<ServerResponse> imgRouter() {
        return RouterFunctions
                .resources("/**", new ClassPathResource("public/"));
    }
}
Toerktumlare
  • 12,548
  • 3
  • 35
  • 54