2017-03-28 50 views
0

我正在期運用springboot創建一個項目,這是沒有錯誤運行的想法,但是,運行app.jar文件,它的運行異常喜歡這個java.io.FileNotFoundException:/ BOOT-INF /班/模板/

java.io.FileNotFoundException: class path resource [templates/] cannot be resol 
ed to absolute file path because it does not reside in the file system: jar:fil 
:/E:/projects/help/target/zhx-help-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/templa 
es/ 
    at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:21 
) 
    at org.springframework.core.io.AbstractFileResolvingResource.getFile(Ab 
tractFileResolvingResource.java:52) 
    at org.springframework.ui.freemarker.FreeMarkerConfigurationFactory.get 
emplateLoaderForPath(FreeMarkerConfigurationFactory.java:338) 
    at org.springframework.ui.freemarker.FreeMarkerConfigurationFactory.cre 
teConfiguration(FreeMarkerConfigurationFactory.java:290) 
    at org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer 
    afterPropertiesSet(FreeMarkerConfigurer.java:116) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBea 
Factory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBea 
Factory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBea 
Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBea 
Factory.createBean(AbstractAutowireCapableBeanFactory.java:483) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getO 
ject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegist 
y.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetB 
an(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBea 
(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory 

springboot版本:1.5.2

期運用彈簧數據的JPA

回答

0

我看你用的freemarker。在Spring引導中,您無法使用正常的File方法獲取您的模板,因爲在運行可執行JAR時無法訪問它們(File無法在JAR內部作爲資源加載)

使用以下方法加載模板文件夾:

cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader(), "templates")); 

完整的示例:

@Configuration 
public class FreemarkerConfiguration { 

    @Bean 
    public freemarker.template.Configuration freemarkerConfig() throws IOException { 
     freemarker.template.Configuration cfg = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_23); 
     cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader(), "templates")); 
     cfg.setDefaultEncoding("UTF-8"); 
     cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); 
     cfg.setLogTemplateExceptions(false); 
     return cfg; 
    } 

} 
0

FreeMarkerConfigurationFactory.class

protected TemplateLoader getTemplateLoaderForPath(String templateLoaderPath) { 
    if(this.isPreferFileSystemAccess()) { 
     try { 
      Resource ex = this.getResourceLoader().getResource(templateLoaderPath); 
      File file = ex.getFile(); 
      if(this.logger.isDebugEnabled()) { 
       this.logger.debug("Template loader path [" + ex + "] resolved to file path [" + file.getAbsolutePath() + "]"); 
      } 

      return new FileTemplateLoader(file); 
     } catch (IOException var4) { 
      if(this.logger.isDebugEnabled()) { 
       this.logger.debug("Cannot resolve template loader path [" + templateLoaderPath + "] to [java.io.File]: using SpringTemplateLoader as fallback", var4); 
      } 

      return new SpringTemplateLoader(this.getResourceLoader(), templateLoaderPath); 
     } 
    } else { 
     this.logger.debug("File system access not preferred: using SpringTemplateLoader"); 
     return new SpringTemplateLoader(this.getResourceLoader(), templateLoaderPath); 
    } 
} 

這樣做日誌槓桿信息

<logger name="org.springframework.web" level="INFO"/>