[springboot web series] Webflux static resource configuration and access

Time:2021-7-24

[springboot web series] Webflux static resource configuration and access

[springboot web series] Webflux static resource configuration and access

The last blog post introduced the static resource access of spring MVC. Is the static resource access posture consistent in Webflux

<!– more –>

1. Default configuration

Like the default configuration of springboot, Webflux is the sameclasspath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

That is, static files can be directly accessed by placing them in these four directories

1. Project demonstration

Create a springboot project and add dependencies (the version used in this article is:2.2.1-RELEASE)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

Add directory under resource pathstatic, add two HTML files under the directory, as shown below

[springboot web series] Webflux static resource configuration and access

To implement the startup class, you can directly access the static resources through the full URL without adding additional logic

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

[springboot web series] Webflux static resource configuration and access

Mainly observe the above three requests and put them inindex.htmlIt cannot be accessed directly because its directory is not in the default four static resource paths

2. URL mapping

The above is accessed directly through the static resource file name. Can Webflux implement the spring MVC method of returning the view according to the view name?

@Controller
public class ViewAction {
    @GetMapping(path = "a")
    public String a() {
        return "a.html";
    }
}

Direct access, the result is 500, and the name is not founda.htmlView of

[springboot web series] Webflux static resource configuration and access

If this method doesn’t work, use the routing method of Webflux

@Bean
public RouterFunction<ServerResponse> indexRouter() {
    return RouterFunctions.route(RequestPredicates.GET("/b"),
                    request -> ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue("b.html");
}

[springboot web series] Webflux static resource configuration and access

2. Custom configuration path

If we want to specify a custom path, can we modify the configuration or code and set the mapping as spring MVC does?

In the resource directory, add two new folders, O1 and O2

[springboot web series] Webflux static resource configuration and access

1. Configuration modification

For example, in spring MVC, modify the static resource configuration

spring:
  resources:
    static-locations: classpath:/o1/,classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

Then visit/o1.html, it is found that 404, this method of directly modifying the configuration does not work!!!

[springboot web series] Webflux static resource configuration and access

2. Add mapping to webfluxconfigurer

Refer to the official document: web-reactive.html#weblux-config-static-resources

Directly modify the startup class to realizeWebFluxConfigurerInterface, add resource mapping manually

@SpringBootApplication
public class Application implements WebFluxConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/o2/");
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Then visit/o2.html

[springboot web series] Webflux static resource configuration and access

3. @ value mode

In addition to the above manual mapping methods, there is also a non mainstream method, such as

@Bean
public RouterFunction<ServerResponse> indexRouter(@Value("classpath:/index.html") final Resource indexHtml,
        @Value("classpath:/self/s.html") final Resource sHtml) {
    return RouterFunctions.route(RequestPredicates.GET("/index"),
            request -> ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(indexHtml))
            .andRoute(RequestPredicates.GET("/s"),
                    request -> ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(sHtml));
}

Please note the two files above,s.html, index.htmlAre not in the default static resource directory

[springboot web series] Webflux static resource configuration and access

3. Summary

This paper gives the static resource access posture of Webflux, which is different from spring MVC

  • When URL mapping, directly return the view name, and you will be promptedCould not resolve view with name xxx
  • By modifying the configurationspring.resources.static-locationsThe specified new static resource directory is invalid

In Webflux, implementation is recommendedWebFluxConfigureInterface mode, overrideaddResourceHandlersMethod to customize resource path mapping

You can also use the@ValueTo route manually

2. Other

0. Project

  • Project: https://github.com/liuyueyi/spring-boot-demo
  • Source code: https://github.com/liuyueyi/spring-boot-demo/blob/master/spring-boot/200-webflux

1. A gray blog

The above contents are only the words of one family. Due to limited personal ability, it is inevitable that there are omissions and mistakes. If you find a bug or have better suggestions, you are welcome to criticize and correct and be grateful

The following is a gray personal blog, which records all blog posts in study and work. Welcome to visit

  • A personal blog https://blog.hhui.top
  • A gray blog spring special blog http://spring.hhui.top

[springboot web series] Webflux static resource configuration and access