How to serve static content using Webflux?

I am learning WebFlux and I would like to know how to serve static content on MicroService using WebFlux, but I did not find information to do this.

+12
static-content spring-webflux
source share
4 answers

try it

RouterFunction router = resources("/**", new ClassPathResource("public/")); 
+9
source share

Juan Medina is right. I just want to make it even more understandable and provide a link .

In fact, you just need to add the RouterFunction component to handle static resources. You do not need to implement your own RouterFunction, because RouterFunctions.resources("/**", new ClassPathResource("static/")); gives what you want.

All I do is add this piece of code:

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

All unrecognized requests will go to the static router.

+6
source share

Spring Web Flux and Configuring Public Static Web Resources

  • Put the public static web resources in the public-web-resources folder:

     ./src/main/public-web-resources 
  • configure Spring Boot 2.0 , application.yaml :

     spring.main.web-application-type: "REACTIVE" spring.webflux.static-path-pattern: "/app/**" spring.resources.static-locations: - "classpath:/public-web-resources/" 
  • configure maven-resources-plugin , pom.xml :

     <build> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <id>copy-resources</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <resources> <resource> <directory>src/main/public-web-resources</directory> <filtering>true</filtering> </resource> </resources> <outputDirectory>${basedir}/target/classes/public-web-resources</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.0.0.BUILD-SNAPSHOT</version> </plugin> </plugins> </build> 
+5
source share

Thanks to wildloop for me with the following properties:

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

Spring boot adds the following log line:

 15:51:43.776 INFO Adding welcome page: class path resource [public-web-resources/index.html] - WebMvcAutoConfiguration$WelcomePageHandlerMapping.<init> 

And it works as a welcome page for http: // localhost: port / myapp /

Wish there was a way to call it on /myapp/docs

+2
source share

All Articles