Spring Boot Freemarker 从 2.2.0 升级失败

Gyu*_*Lee 3 java spring freemarker kotlin spring-boot

将 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"
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

pom.xml的基本上如下:

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

在升级到 Spring Boot 2.2.0.RELEASE 之前,运行得非常好
这里有什么问题?

  • 根据规则,这是一个自我回答的问题,以防止其他可怜的灵魂像我一样受苦。

Gyu*_*Lee 10

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

可以在此处找到更改此内容的提交。它旨在解决这个问题,即 freemarker 默认设置应该更安全,即启用自动 HTML 转义。

2.2.0.RELEASE可以在此处找到升级前应阅读的完整变更日志。