【问题标题】:Spring boot executable jar can not resolve freemarker templateSpring Boot 可执行 jar 无法解析 freemarker 模板
【发布时间】:2018-02-25 04:59:27
【问题描述】:

我正在学习使用 Spring Boot 和 Java 构建 Web 应用程序。当我通过 Spring Tool Suite 运行我的应用程序时,我的应用程序可以正常工作,但是在我使用 bootRepackage 构建可执行 jar 并运行它之后,它无法解析 freemarker 视图。

我不确定出了什么问题。任何帮助,将不胜感激。

以下是我的freemarker相关的application.properties,

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/

我的 jar 结构,

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

我的控制器,

@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);
        }
}

我确认我正在点击日志步骤,所以我认为问题在于解决视图,但我可能错了并且遗漏了其他东西。如果您需要更多信息,请告诉我。

再次感谢!

最好, 农历

编辑 Gradle 文件,

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')
}

编辑 3, 进一步更新,

所以我附加了远程调试器,我发现 spring 正在使用 ContentNegotiatingViewResolver 将视图解析为 InternalResourceView,但是当我通过 spring 工具套件执行时,它正确解析为 FreemarkerView。 我希望这可以帮助某人缩小我的问题范围。我会看看我是否可以通过调试器同时到达其他任何地方。

【问题讨论】:

  • 您应该在此类问题中也包含错误消息(堆栈跟踪)。
  • 不幸的是,我没有收到任何错误,所以不知道哪里出错了。运行打包的 jar 时是否有可以打开的调试或跟踪标志?
  • 分解您的 jar 文件以确保 jar 文件位于 lib 目录中。
  • 您说您没有收到错误消息(我假设您已经检查了日志和浏览器中返回的响应),但是您会得到什么,会发生什么?
  • @JCCarrilo 我今天回家后会试试看。

标签: spring spring-mvc spring-boot freemarker


【解决方案1】:

也许 freemarker jar 文件未在您的 Spring Boot 应用程序模块中指定为依赖项。不确定您是在运行 maven 还是 gradle,但请确保您的构建中包含 freemarker 库。

查看下载@https://mvnrepository.com/artifact/org.freemarker/freemarker

【讨论】:

  • 您好,感谢您的回复。我正在使用gradle。我尝试在 gradle 依赖项中明确包含 org.freemarker,但仍然是同样的问题。 :/我已经用我的 gradle 文件更新了原始帖子。如果您发现有问题,请您看一下吗?
  • 显示你的jar文件的内容:tar -tvf target/{your_jar_file}
猜你喜欢
  • 2017-07-08
  • 2015-12-30
  • 1970-01-01
  • 2017-11-21
  • 2020-07-25
  • 2014-11-27
  • 2018-05-26
  • 2015-01-30
  • 2014-05-11
相关资源
最近更新 更多