7

I have a very simple webflux demo application with freemarker which has got following files:

1.WebFluxDemoApplication.java

@SpringBootApplication
public class WebFluxDemoApplication {

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

    @Controller
    class HomeController {
        @GetMapping("/hello")
        public String hello() {
            return "index";
        }
    }
}

2.index.ftl(located under classpath:/templates)

<html>
    <head>
        <title>demo</title>
    </head>
    <body></body>
</html>

3.application.yml(without any configuration)

4.pom.xml(with spring-boot-starter-freemarker and spring-boot-starter-webflux)

I can get normal page via http://localhost:8080/hello using these files, but if I add @EnableWebFlux to WebFluxDemoApplication, there's an error shows java.lang.IllegalStateException: Could not resolve view with name 'index'.

I notice that the Spring WebFlux's official guide states to use @EnableWebFlux to configure freemarker. Actually it works for template files, but there seems something wrong with static resources.

For example, I put a main.js file under classpath:/templates/js/, and add <script src="/js/main.js"></script> in index.ftl, then I get an error says WARN 3046 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://localhost:8080/js/main.js]: Response status 404 with reason "No matching handler"

UPDATE For static resources problem, I found a solution which is to add a RouterFunction that resolves the static resources as the post stated.

Tonny Tc
  • 852
  • 1
  • 12
  • 37
  • I just ran into this myself, with the static resources. There is an open Spring issue right now, https://github.com/spring-projects/spring-boot/issues/9785. I ended up working with this commit, https://github.com/koju/nepse-data/commit/00bd1d6e3d81807a3760ef677e5d70c91c41f0d2 to get started. I haven't added in JS yet, but that got the main index.html working. – Brian Aug 14 '18 at 18:54
  • If without `@EnableWebFlux`, all static resources can be loaded. I'm afraid the case is different with the issue... – Tonny Tc Aug 15 '18 at 00:55

1 Answers1

9

As stated in the Spring Boot reference documentation, @EnableWebFlux will tell Spring Boot that you wish to take full control over the WebFlux configuration and disable all auto-configuration for this (including static resources).

@EnableWebFlux doesn't configure Freemarker, it actually sets up the whole WebFlux infrastructure. In the case of Spring Boot, adding the spring-boot-starter-freemarker as a dependency (and optionally configuring it through configuration properties) is all you need.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • 4
    If so, I think the name of `@EnableWebFlux` itself is confusing -- it not enables the webflux function but disables all auto-configuration... – Tonny Tc Aug 18 '18 at 09:32
  • 1
    EnableWebflux is a Spring Framework annotation that you can use to set up a WebFlux application from scratch. Spring Boot is doing that for you. There is no auto-configuration in Spring Framework. I know things can be confusing when looking at the documentation, so feel free to suggest changes by creating a Spring Framework or Spring Boot issue on their respective trackers. – Brian Clozel Aug 18 '18 at 10:56