How to serve static content using Webflux?
Asked Answered
S

5

27

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.

Sirenasirenic answered 25/4, 2017 at 22:30 Comment(2)
Are you using Spring Boot 2.0 or are ou bootstrapping webflux yourself?Bacterium
Hi Brian, I am using Spring Boot 2.0 with Webflux. I would like to have the reactive features from Webflux but in the same app, has the opportunity to serve some static files.Sirenasirenic
G
17

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

Gann answered 27/4, 2017 at 22:39 Comment(4)
It is important to end resource path with slash (/) so it is considered a root for resources tree. Can also use new FileSystemResource("path/") to serve from user's filesystem.Paintbrush
Where is this method resources()? I am bootstrapping myself not using Spring Boot.Christychristye
@Christychristye you can found it inside org.springframework.web.reactive.function.serverThe
That is it! Thanks! Note: just don't forget to specify a name of your static resource in the URL, like, localhost:8080/index.htmlDesign
P
15

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

In fact, you just have to add a RouterFunction bean to handle static resources. You don't have to implement your own RouterFunction because RouterFunctions.resources("/**", new ClassPathResource("static/")); gives what you want.

All I do is to add this piece of code:

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

Whatever unrecoginized requests will fall into the static router.

Parapodium answered 20/4, 2018 at 8:30 Comment(2)
in which class should I put this function?Volatilize
any @org.springframework.context.annotation.Configuration annotated class for a typical spring applicationTerbecki
C
7

Spring Web Flux & public static web resources configuration

  • put public static web resources into 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>
    
Cookout answered 12/6, 2017 at 12:29 Comment(1)
Note: you must not have @EnableWebFlux annotation if specifying the spring.webflux.static-path-pattern.Menderes
W
3

Thanks wildloop worked for me with following properties:

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

Spring boot adds 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 welcome page for http://localhost:port/myapp/

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

Weald answered 9/12, 2017 at 16:15 Comment(0)
P
0

I struggled to host static content outside of the .jar executable. With spring boot mvc you can just create a public directory next to the .jar and it will serve the files for you. With spring webflux that is not the case. The solutions mentioned here only works if you place the static files in the resources/public folder and then build the .jar.

I got spring webflux to serve static content without including it in the jar with the following config:

spring:
  application:
    name: spring-cloud-gateway
  webflux.static-path-pattern: "/**"
  resources.static-locations: "file:public/"

Wit this you can build the webflux jar and add the static files later on deployment.

Polyzoan answered 23/10, 2019 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.