Static Content in Spring WebFlux – Spring WebFlux中的静止内容

最后修改: 2019年 3月 2日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Sometimes, we have to serve static content in our web applications. It might be an image, HTML, CSS, or a JavaScript file.

有时,我们必须在我们的网络应用中提供静态内容。它可能是一个图片、HTML、CSS或JavaScript文件。

In this tutorial, we’ll show how to serve static content using Spring WebFlux. We also assume that our web application will be configured using Spring Boot.

在本教程中,我们将展示如何使用Spring WebFlux提供静态内容。我们还假设我们的Web应用程序将使用Spring Boot进行配置。

2. Overriding the Default Configuration

2.覆盖默认配置

By default, Spring Boot serves static content from the following locations:

默认情况下,Spring Boot从以下位置提供静态内容。

  • /public
  • /static
  • /resources
  • /META-INF/resources

All files from these paths are served under the /[resource-file-name] path.

来自这些路径的所有文件都在/[资源-文件名]/em>路径下提供。

If we want to change the default path for Spring WebFlux, we need to add this property to our application.properties file:

如果我们想改变Spring WebFlux的默认路径,我们需要在application.properties文件中添加这个属性。

spring.webflux.static-path-pattern=/assets/**

Now, the static resources will be located under /assets/[resource-file-name].

现在,静态资源将位于/assets/[resource-file-name]下。

Please note that this won’t work when the @EnableWebFlux annotation is present.

请注意,当@EnableWebFlux注解存在时,这个不会工作。

3. Routing Example

3.路由实例

It’s also possible to serve static content using the WebFlux routing mechanism.

也可以使用WebFlux的路由机制来提供静态内容。

Let’s look at an example of a routing definition to serve the index.html file:

让我们看看一个为index.html文件服务的路由定义的例子。

@Bean
public RouterFunction<ServerResponse> htmlRouter(
  @Value("classpath:/public/index.html") Resource html) {
    return route(GET("/"), request
      -> ok().contentType(MediaType.TEXT_HTML).syncBody(html)
    );
}

We can also serve static content from custom locations with the help of RouterFunction.

RouterFunction的帮助下,我们还可以从自定义位置提供静态内容。

Let’s see how to serve images from the src/main/resources/img directory using the /img/** path:

让我们看看如何使用/img/**路径从src/main/resources/img目录下提供图片。

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

4. Custom Web Resources Path Example

4.自定义网络资源路径示例

Another way to serve static assets stored in custom locations, instead of the default src/main/resources path, is to use the maven-resources-plugin and an additional Spring WebFlux property.

另一种方法是使用maven-resources-plugin和一个额外的Spring WebFlux属性,来提供存储在自定义位置的静态资产,而不是默认的src/main/resources路径。

First, let’s add the plugin to our pom.xml:

首先,让我们把这个插件添加到我们的pom.xml

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <resources>
                    <resource>
                        <directory>src/main/assets</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
                <outputDirectory>${basedir}/target/classes/assets</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

Then, we simply need to set the static locations property:

然后,我们只需要设置静态位置属性。

spring.resources.static-locations=classpath:/assets/

After these actions, the index.html will be available under the http://localhost:8080/index.html URL.

经过这些操作,index.html将在http://localhost:8080/index.html URL下可用。

5. Conclusion

5.结论

In this article, we learned how to serve static content in Spring WebFlux.

在这篇文章中,我们学习了如何在Spring WebFlux中提供静态内容。

As always, the sample code presented is available over on GitHub.

一如既往,所介绍的示例代码可在GitHub上获得。