Thymeleaf Template Resolver prefix with Gradle

Viewed 2148

I'm trying to deploy a Spring Boot application built with Gradle in AWS Elastic Beanstalk but I keep getting this error:

2017-07-05 13:47:17.913 ERROR 2804 --- [nio-5000-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates//articulos/index.html]")] with root cause

java.io.FileNotFoundException: class path resource [templates//articulos/index.html] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) ~[spring-core-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
    at org.thymeleaf.spring4.templateresource.SpringResourceTemplateResource.reader(SpringResourceTemplateResource.java:103) ~[thymeleaf-spring4-3.0.3.RELEASE.jar!/:3.0.3.RELEASE]
...

This is my Configuration class:

@Bean
public ViewResolver viewResolver() {
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    resolver.setTemplateEngine(templateEngine());
    resolver.setCharacterEncoding("UTF-8");

    return resolver;
}

private TemplateEngine templateEngine() {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.addDialect(new Java8TimeDialect());
    engine.addTemplateResolver(templateResolver());
    engine.getConfiguration();

    return engine;
}

@Bean
public SpringResourceTemplateResolver templateResolver() {
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setCacheable(false);
    templateResolver.setPrefix("classpath:/templates/");
    templateResolver.setSuffix(".html");
    return templateResolver;
}

@Bean
public UrlTemplateResolver urlTemplateResolver() {
    return new UrlTemplateResolver();
}

And the generated folder structure goes like this: Gradle build folder structure

I've tried changing the prefix of the TemplateResolver to:

  • /templates
  • templates
  • templates/
  • /templates/
  • classpath:templates/
  • classpath:/templates/

My app is packed in a WAR file, with the following Gradle configuration:

buildscript {
    ext {
        springBootVersion = '1.5.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    providedRuntime
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.flywaydb:flyway-core')
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile ("org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.0.RELEASE")
    compile ("org.thymeleaf.extras:thymeleaf-extras-java8time:3.0.0.RELEASE")
    compile ("io.github.jpenren:thymeleaf-spring-data-dialect:3.1.1")
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('org.springframework.boot:spring-boot-devtools')
    runtime('mysql:mysql-connector-java')
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

ext["thymeleaf.version"] = "3.0.3.RELEASE"

And this is the tree output for the unpacked WAR file, the folder 'templates' seems to be in the classpath:

.
|-- META-INF
|-- WEB-INF
|   |-- classes
|   |   |-- ar
|   |   |   `-- com
|   |   |       `-- reweb
|   |   |           `-- data
|   |   |               `-- repository
|   |   |-- db
|   |   |   `-- migration
|   |   |-- static
|   |   |   |-- bootstrap
|   |   |   |   |-- css
|   |   |   |   |-- fonts
|   |   |   |   `-- js
|   |   |   |-- css
|   |   |   |-- img
|   |   |   |-- jquery
|   |   |   |   `-- ui
|   |   |   |       |-- css
|   |   |   |       `-- js
|   |   |   `-- js
|   |   |       `-- articulos
|   |   `-- templates
|   |       |-- articulos
|   |       |   |-- aplicaciones

With no luck, any idea? Thanks

1 Answers
Related