【问题标题】:How to serve static content using Webflux?如何使用 Webflux 提供静态内容?
【发布时间】:2017-09-23 03:21:18
【问题描述】:

我正在学习 webflux,我想知道如何使用 webflux 在微服务上提供静态内容,但我没有找到相关信息。

【问题讨论】:

  • 您使用的是 Spring Boot 2.0 还是您自己引导 webflux?
  • 嗨,Brian,我正在使用带有 Webflux 的 Spring Boot 2.0。我想拥有 Webflux 的响应式功能,但在同一个应用程序中,有机会提供一些静态文件。

标签: spring-webflux static-content


【解决方案1】:

我很难在.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 并稍后在部署时添加静态文件。

【讨论】:

    【解决方案2】:

    试试这个

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

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

    【讨论】:

    • 资源路径必须以斜线 (/) 结束,因此它被视为资源树的根。也可以使用new FileSystemResource("path/") 从用户的文件系统服务。
    • 这个方法resources()在哪里?我正在引导自己不使用 Spring Boot。
    • @MohamedSanaulla 你可以在org.springframework.web.reactive.function.server中找到它
    • 就是这样!谢谢!注意:不要忘记在 URL 中指定静态资源的名称,例如 localhost:8080/index.html
    【解决方案3】:

    胡安·梅迪纳是对的。我只是想让它更清楚并提供reference link

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

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

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

    任何无法识别的请求都会落入静态路由器。

    【讨论】:

    • 这个函数应该放在哪个类?
    • 任何 @org.springframework.context.annotation.Configuration 注释类,用于典型的 Spring 应用程序
    【解决方案4】:

    感谢 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 上调用它

    【讨论】:

      【解决方案5】:

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

      • 将公共静态网络资源放入public-web-resources文件夹:

        ./src/main/public-web-resources
        
      • 配置Spring Boot 2.0application.yaml:

        spring.main.web-application-type: "REACTIVE"
        spring.webflux.static-path-pattern: "/app/**"
        spring.resources.static-locations:
          - "classpath:/public-web-resources/"
        
      • 配置ma​​ven-resources-pluginpom.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注解。
      猜你喜欢
      • 2017-12-22
      • 2018-12-25
      • 2012-01-28
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 2011-10-14
      相关资源
      最近更新 更多