如何使用Webflux提供静态内容?

25

我正在学习WebFlux,并且想知道如何在使用WebFlux的微服务中提供静态内容,但我找不到相关信息。


你是在使用Spring Boot 2.0还是自己启动Webflux? - Brian Clozel
嗨,Brian,我正在使用Spring Boot 2.0和Webflux。我想要从Webflux中获得反应式特性,但在同一个应用程序中,也有机会提供一些静态文件服务。 - jabrena
5个回答

16

试试这个

RouterFunction router = resources("/**", new ClassPathResource("public/"));

更新: 不要忘记在从外部访问时在URL中指定静态文件的名称,例如localhost:8080/index.html


重要的是要用斜杠(/)结束资源路径,这样它就被视为资源树的根。也可以使用 new FileSystemResource("path/") 从用户的文件系统中提供服务。 - Yaroslav Stavnichiy
这个方法 resources() 在哪里?我正在自己引导,不使用Spring Boot。 - MohamedSanaulla
@MohamedSanaulla,你可以在org.springframework.web.reactive.function.server中找到它。 - Vladlen Gladis
1
就是这样了!谢谢!注意:不要忘记在URL中指定静态资源的名称,例如localhost:8080/index.html。 - yuranos

13

Juan Medina是正确的。我只是想让它更清晰,并提供一个参考链接

实际上,您只需添加一个RouterFunction bean来处理静态资源。您不必实现自己的RouterFunction,因为RouterFunctions.resources("/**", new ClassPathResource("static/"));可以达到你想要的效果。

我所做的就是添加这段代码:

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

任何未被识别的请求都会落入静态路由器中。


我应该把这个函数放在哪个类里? - RezKesh
任何一个带有@org.springframework.context.annotation.Configuration注解的类都可以用于典型的Spring应用程序。 - Dave Ankin

7

Spring Web Flux和公共静态Web资源配置

  • 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>
    

注意:如果指定了 spring.webflux.static-path-pattern,则不得使用 @EnableWebFlux 注释。 - Kane

3
感谢wildloop,以下是适用于我的属性:
spring.webflux.static-path-pattern: "/**"
spring.resources.static-locations: "classpath:/public-web-resources/"

Spring Boot 添加以下日志行:

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

这是欢迎页面,适用于 http://localhost:port/myapp/

希望有一种方法可以在/myapp/docs上调用它


0

我曾经苦于将静态内容托管到.jar可执行文件之外。使用Spring Boot MVC,您只需在.jar旁边创建一个public目录,它就会为您提供文件服务。但是,对于Spring WebFlux来说并非如此。这里提到的解决方案仅在您将静态文件放置在resources/public文件夹中并构建.jar时才有效。

我通过以下配置使Spring WebFlux无需将静态内容包含在jar中即可提供静态内容服务:

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

使用这个工具,你可以构建WebFlux的jar文件,并在部署后添加静态文件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接