How to serve static content using Webflux?

2019-02-08 07:32发布

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.

4条回答
乱世女痞
2楼-- · 2019-02-08 07:57

Try this

RouterFunction router = resources("/**", new ClassPathResource("public/"));
查看更多
叼着烟拽天下
3楼-- · 2019-02-08 08:05

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>
    
查看更多
该账号已被封号
4楼-- · 2019-02-08 08:05

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

查看更多
成全新的幸福
5楼-- · 2019-02-08 08:16

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.

查看更多
登录 后发表回答