I am learning webflux and I would like to know how to serve static content on a MicroService using webflux but I didn´t find information to do it.
Spring Boot comes with a pre-configured implementation of ResourceHttpRequestHandler to facilitate serving static resources. By default, this handler serves static content from any of the /static, /public, /resources, and /META-INF/resources directories that are on the classpath.
Moreover, Spring WebFlux supports reactive backpressure, so we have more control over how we should react to fast producers than both Spring MVC Async and Spring MVC. Spring Flux also has a tangible shift towards functional coding style and declarative API decomposition thanks to Reactor API behind it.
The file is located in the src/main/resources/static directory, which is a default directory where Spring looks for static content. In the link tag we refer to the main. css static resource, which is located in the src/main/resources/static/css directory. In the main.
Step 1: Move images folder from src/main/resources/static/images to src/main/webapp/WEB-INF/images. Step 2: Look up SpringBootMainApplication. java add code @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // Register resource handler for images registry.
Try this
RouterFunction router = resources("/**", new ClassPathResource("public/"));
UPDATE: Don't forget to specify a name of the static file in the URL when accessing it from outside, like localhost:8080/index.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With