Spring Boot Freemarker fails from 2.2.0 upgrade

Gyuhyeon Lee :

Upon upgrading the spring-boot-parent to 2.2.0.RELEASE, my freemarker based Spring Boot Web application fails to properly serve requests.

I have a @Controller that serves /hello with src/main/resources/templates/hello.ftl.

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

upon request, it just lands on an error page saying There was an unexpected error (type=Not Found, status=404)..

Error stacktrace doesn't say much. It just says org.springframework.web.servlet.resource.ResourceHttpRequestHandler: Resource not found.

My pom.xml is basically as follows:

<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>

It worked perfectly fine up until upgrading to Spring Boot 2.2.0.RELEASE.
What's the problem here?

  • This is a self-answering question as per rules to prevent other poor souls from suffering like I did.
Gyuhyeon Lee :

This is due to a breaking change in Spring Boot 2.2.0 with default freemarker suffixes.
Freemarker files should now end with .ftlh rather than .ftl.
.ftlh enables the HTML auto-escape feature.

The commit that changed this can be found here. It aims to fix this issue that freemarker default settings should be safer, which is enabling automatic HTML escapes.

The full changelog for 2.2.0.RELEASE that you should read before upgrading can be found here.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324300&siteId=1