简体   繁体   中英

Spring boot executable jar can not resolve freemarker template

I am learning web application building with spring boot and java. I've got my app working when I run it through Spring Tool Suite but after I build executable jar using bootRepackage and run it, It's not able to resolve the freemarker views.

I am not sure what's wrong. Any help would be appreciated.

Following is my application.properties related to freemarker,

spring.http.encoding.charset=UTF-8
spring.freemarker.cache=false 
spring.freemarker.charset=utf-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.enabled=true 
spring.freemarker.suffix=.html 
spring.freemarker.template-loader-path=classpath:/templates/,classpath:/templates/web/

My jar structure,

BOOT-INF
  classes
       com
       scss
       static
       templates
           web
               story.html
           app
       application.properties
       log4j2.xml
META-INF
org

my controller,

@Controller
public class HomeController {

        @Autowired
        private AppLog appLogger; 

        @RequestMapping("/")
        public ModelAndView Index(HttpServletRequest request) {
            appLogger.log(Level.ERROR,AppLogSource.Web, "Reached Controller", null);
            String testAttribute = request.getAttribute("com.demo.test").toString();
            Map<String, String> vm = new HashMap<String, String>();
            vm.put("testAttribute", testAttribute);
            return new ModelAndView("/web/story", vm);
        }
}

I verified that I am hitting the log step so I think issue is in resolving the view but I could be wrong and missing something else. So let me know if you need more info.

Thanks again!

Best, Mrunal

edit Gradle File,

buildscript {
    ext {
        springBootVersion = '1.4.1.RELEASE'
    }
    repositories {
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("com.moowork.gradle:gradle-node-plugin:1.2.0")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'com.moowork.node'
apply plugin: 'com.moowork.grunt'

jar {
    baseName = 'testDemo'
    version = '0.0.1'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

node {
  version = '6.11.3'
  npmVersion = '3.10.10'
  download = true
}

task gruntMinifyJs(type: GruntTask){
  args=['minifyJs', '--debug']
}

task gruntMinifyCss(type: GruntTask){
  args=['minifyCss', '--debug']
}

task buildFrontEnd(type: GruntTask) {
  args = ['default', '--debug']
}

npmInstall.dependsOn(nodeSetup)
buildFrontEnd.dependsOn(npmInstall)
gruntMinifyCss.dependsOn(npmInstall)
gruntMinifyJs.dependsOn(npmInstall)
build.dependsOn(buildFrontEnd)

configurations {
 all*.exclude group: 'ch.qos.logback', module:'logback-classic'
 all*.exclude group: 'ch.qos.logback', module:'logback-core'
}

dependencies {
    compile('org.springframework.boot:spring-boot-devtools')
    compile('org.springframework.boot:spring-boot-starter-freemarker')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter:1.4.1.RELEASE'){
        exclude group:'org.springframework.boot', module:'spring-boot-starter-logging'
    }

    compile('org.springframework.boot:spring-boot-starter-jdbc'){
        exclude group:'org.apache.tomcat', module:'tomcat-jdbc'
    }
    compile('mysql:mysql-connector-java')
    compile('com.zaxxer:HikariCP-java6:2.3.13')
    compile('org.springframework.boot:spring-boot-starter-log4j2:1.4.1.RELEASE')

    compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.8')
    compile('com.google.zxing:core:3.3.0')
    compile('org.antlr:antlr4-runtime:4.5')

    testCompile('org.springframework.boot:spring-boot-starter-test')
}

edit 3 , further updates,

So I attached remote debugger and I found that spring is using ContentNegotiatingViewResolver to resolve the view as InternalResourceView but when I execute through spring tool suite it resolves properly to FreemarkerView. I hope this helps someone to narrow down my issue. I'll see if I can get anywhere else in mean time by stepping through debugger.

Perhaps the freemarker jar file is not specified as a dependency in your spring boot application module. Unsure if you are running maven or gradle but do make sure you have the freemarker library included in your build.

See downloads @ https://mvnrepository.com/artifact/org.freemarker/freemarker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM