Ton*_* Tc 6 spring freemarker spring-boot 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";
}
}
}
Run Code Online (Sandbox Code Playgroud)
2.index.ftl(位于classpath:/templates下)
<html>
<head>
<title>demo</title>
</head>
<body></body>
</html>
Run Code Online (Sandbox Code Playgroud)
3.application.yml(无需任何配置)
4.pom.xml(带spring-boot-starter-freemarker和spring-boot-starter-webflux)
我可以通过http://localhost:8080/hello使用这些文件获得正常页面,但是如果我添加@EnableWebFlux到WebFluxDemoApplication,则会显示错误java.lang.IllegalStateException: Could not resolve view with name 'index'.
我注意到Spring WebFlux 的官方指南指出@EnableWebFlux用于配置 freemarker。实际上它适用于模板文件,但静态资源似乎有问题。
例如,我在 下放了一个main.js文件,然后在 中classpath:/templates/js/添加,然后我得到一个错误说<script src="/js/main.js"></script>index.ftlWARN 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解决静态资源的方法,如帖子所述。
如Spring Boot 参考文档中所述,@EnableWebFlux将告诉 Spring Boot 您希望完全控制 WebFlux 配置并为此禁用所有自动配置(包括静态资源)。
@EnableWebFlux不配置 Freemarker,它实际上设置了整个 WebFlux 基础设施。在 Spring Boot 的情况下,spring-boot-starter-freemarker您只需要添加作为依赖项(并可选择通过配置属性对其进行配置)。
| 归档时间: |
|
| 查看次数: |
5986 次 |
| 最近记录: |