spring - @EnableWebFlux 注解的作用是什么

标签 spring spring-boot freemarker spring-webflux

我有一个非常简单的带有 freemarker 的 webflux 演示应用程序,它有以下文件:

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 (位于类路径:/模板下)
<html>
    <head>
        <title>demo</title>
    </head>
    <body></body>
</html>

3.application.yml (无需任何配置)

4.pom.xml (与 spring-boot-starter-freemarkerspring-boot-starter-webflux )

我可以通过 http://localhost:8080/hello 获得正常页面使用这些文件,但如果我添加 @EnableWebFluxWebFluxDemoApplication , 有错误显示 java.lang.IllegalStateException: Could not resolve view with name 'index'.

我注意到 Spring WebFlux's official guide要使用的状态 @EnableWebFlux配置freemarker。实际上它适用于模板文件,但静态资源似乎有问题。

例如,我放了一个 main.js文件下 classpath:/templates/js/ ,并添加 <script src="/js/main.js"></script>index.ftl ,然后我收到一条错误消息 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"

更新
对于静态资源问题,我找到了一个解决方案,就是添加一个 RouterFunction将静态资源解析为 post说。

最佳答案

the Spring Boot reference documentation, @EnableWebFlux will tell Spring Boot that you wish to take full control over the WebFlux configuration 中所述并为此禁用所有自动配置(包括静态资源)。
@EnableWebFlux不配置 Freemarker,它实际上设置了整个 WebFlux 基础设施。在 Spring Boot 的情况下,添加 spring-boot-starter-freemarker作为依赖项(并可选择通过配置属性配置它)就是您所需要的。

关于spring - @EnableWebFlux 注解的作用是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51843344/

相关文章:

java - 如何创建跨服务域模型映射?

java - FreeMarker 模板中的 if-else

java - Web 上不再提供 Spring Beans Schema?

spring - 无法启动 springboot 管理,java.lang.NoClassDefFoundError : org/springframework/cloud/netflix/zuul/filters/TraceProxyRequestHelper

java - Spring配置文件不从application.properties读取值

Java + Spring 启动 : Downloading image and pass it to a request

javascript - freemarker 与 javascript 集成

replace - FreeMarker ?替换多个值

java - 在 java.util.Set 变量的 Spring @Value 中将默认值设置为 null

java - 使用 Neo4Jconfiguration 连接 Neo4J 与 Spring boot 时发生未经授权的异常