Spring 从 2.2.0 升级引导 Freemarker 失败

Spring Boot Freemarker fails from 2.2.0 upgrade

将 spring-boot-parent 升级到 2.2.0.RELEASE 后,我基于 freemarker 的 Spring Boot Web 应用程序无法正确处理请求。

我有一个 @Controller 服务于 /hellosrc/main/resources/templates/hello.ftl

@Controller
class HelloController() {
    @RequestMapping(value = ["/hello"])
    fun hello(): String {
        return "hello"
    }
}

根据要求,它只是落在一个错误页面上,上面写着 There was an unexpected error (type=Not Found, status=404).

错误堆栈跟踪不多说。它只是说 org.springframework.web.servlet.resource.ResourceHttpRequestHandler: Resource not found

我的pom.xml基本是这样的:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.0.RELEASE</version>
    <relativePath/>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
</dependencies>

在升级到 Spring Boot 2.2 之前一直运行良好。0.RELEASE.
这里有什么问题?

这是由于 Spring Boot 2.2.0 中具有默认 freemarker 后缀的重大更改。
Freemarker 文件现在应该以 .ftlh 而不是 .ftl.
结尾 .ftlh 启用 HTML 自动转义功能。

可以发现更改此内容的提交 here. It aims to fix this issue freemarker 默认设置应该更安全,这会启用自动 HTML 转义。

可以在 here.

中找到您在升级前应该阅读的 2.2.0.RELEASE 的完整更新日志