Spring Boot is unable to locate ftl files in executable jar from cmd - spring-boot

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Cannot find template location(s): [ServletContext resource [/templates]] (please add some templates, check your FreeMarker configuration, or set spring.freemarker.checkTemplateLocation=false)
I kept ftl files in templates folder in resources.
In application.properties I also added these lines
spring.freemarker.template-loader-path= /templates

You have overridden the default Spring Boot property value of:
spring.freemarker.template-loader-path=classpath:/templates
and now you are pointing to a file system directory /templates. Remove your custom property to load the FTL files from the default classpath location.

Related

giving Freemarker resource folder in deployed application for spring boot

I'm trying to give resource folder for Freemarker template below is my bean config
Configuration freeMarkerConfig() throws IOException {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_29);
cfg.setDirectoryForTemplateLoading(new ClassPathResource("ftl").getFile());
return cfg;
}
and I have ftl folder in the resources folder
-- java
-- resources
-- ftl
-- template.ftl
while deploying it works fine in my local machine but fails in my docker container with exception
java.io.FileNotFoundException: class path resource [ftl] cannot be resolved to absolute file
path because it does not reside in the file system: jar:file:!/BOOT-INF/classes!/ftl
I need to keep ftl in the resources folder and give the directory path to configuration. I don't know how to debug further.
Property spring.freemarker.template-loader-path is available.
spring.freemarker.template-loader-path=classpath:/ftl/
Or, use setTemplateLoaderPath("classpath:/ftl/") instead of setDirectoryForTemplateLoading().
I found the issue after repeated searching. Issue is because of this
Classpath resource not found when running as jar

Spring configuration error WEB-INF/servlet-context.xml FileNotFoundException

I have a very simple spring test app. But I get exception even though everything seems to be on order. I might be missing something. Please check the pic to see the project structure and web.xml file contains as well as exception:-
efinitionStoreException: IOException parsing XML document from class path resource [WEB-INF/servlet-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/servlet-context.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
There are two kinds of resources in a servlet environment:
servlet resources - files uner the root of the web application (loaded via ServletContext)
classpath resources - resources on web application's classpath (loaded via ClassLoader)
When Spring is supposed to load its configuration it needs to know which mechanism to use.
classpath:foo/bar.xml - will load as classpath resource
checking WEB-INF/classes, contents of WEB-INF/lib/*.jar and other shared servlet container's classpath locations
when using maven and its project structure, all files from src/main/resources will be placed on classpath
foo/bar.xml - will load as servlet resource
when using maven and its project structure the src/main/webapp folder is the root of your application
TL;DR As I wrote in the comment, either remove classpath: prefix when referencing XML file or move your XML file to src/main/resources and remove the WEB-INF part.

springMVC not able to locate the config directory

my springMVC project not able to locate the config directory
My application directory structure is--
myapplication
+src/javaApplication
+webcontent
+web-inf
+jsp
+lib
+config
while accessing this configuration I am getting an error
SEVERE: Servlet.service() for servlet [springsocial] in context with path [/socialmedia] threw exception [Handler processing failed; nested exception is java.lang.ExceptionInInitializerError] with root cause
java.util.NoSuchElementException: 'babelnet.minEdgeWeight' doesn't map to an existing object
I am trying to use babelnet in springMVC application. where should i put the configuration details in spring please suggest.
thanks in advance.
In general, your file hierarchy should probably be something like this:
/src
/main
/java
/resources
/webapp
/WEB-INF
/test
/java
Configuration files that need be be on the classpath should go into the /src/main/resources directory. Config files that you reference directly can go pretty much anywhere, but probably belong either in the resources folder or the WEB-INF folder, depending on the context of their use. Spring MVC configuration files should go into /src/main/webapp/WEB-INF.

Unable to invoke 'hadoop jar' command for class, which contains Spring persistance unit

The problem is that, the jar file uses Spring ORM for loading the persistance configurations, and based on these configurations, files are moved to suitable folders in HDFS. Now If i use, 'java -cp' instead of 'hadoop jar', it fails to copy to HDFS, with FileSystem error.
While invoking the jar with hadoop jar command (having spring orm injected) the exception is as:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0'
defined in class path resource [applicationContext.xml
Error creating bean with name 'entityManagerFactory' defined in class
path resource [applicationContext.xml]: Invocation of init method
failed; nested exception is java.lang.IllegalStateException:
Conflicting persistence unit definitions for name 'Persistance':
file:/home/user/Desktop/ABC/apnJar.jar,
file:/tmp/hadoop-ABC/hadoop-unjar2841422106164401019/
Caused by: java.lang.IllegalStateException: Conflicting persistence
unit definitions for name 'Persistance'
Seems like Hadoop is unpacking the jar file to some tmp folder, is this really required?
Can we skip this step by any configuration change?
Any thoughts on this are welcome.
If you use "hadoop jar", hadoop will run org.apache.hadoop.util.RunJar. RunJar will unpackage your jar into a temp folder(In your case is /tmp/hadoop-ABC/hadoop-unjar2841422106164401019/) and load it in the current class loader. At last, it will invoke your main class to run your MapReduce application.
Did you add your jar in the CLASSPATH? If so, you will have your jar and the unpackaged folder in the class loader. I think that's why spring complains it.
As a work around, I extracted the configuration xmls from the jar, and placed them in the working directory.
This worked, however looking for the proper solution.
Hence If you are facing the similar issue, remove all configuration xmls and place the jar with only the compiled class files.

Groovy Spring Bean Classpath

I am attempting to run a groovy script on startup for a web application. I am first trying to follow an example from the book Spring Recipes.
I have a SimpleInterestCalculator in the following package - com.apress.springrecipes.interest
The beans package is in src/test/resources in a package com.wer.com, and that's where the bean is.
My entry in that is
<lang:groovy id="interestCalculator" script-source="classpath:com/apress/springrecipes/interest/SimpleInterestCalculator.groovy">
<lang:property name="rateCalculator" value="rateCalculator" />
</lang:groovy>
I am getting the following error.
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'simpleInterestCalculator':
Could not determine scripted object type for GroovyScriptFactory:
script source locator [classpath:src/com/apress/springrecipes/interest/SimpleInterestCalculator.groovy];
nested exception is java.io.FileNotFoundException:
class path resource [src/com/apress/springrecipes/interest/SimpleInterestCalculator.groovy] cannot be opened because it does not exist
The error you are getting is a FileNotFoundException. This means it is not finding the script file itself. If the file is present, then it may not be on the classpath at runtime.
Check your bin/out directory in your IDE to see if the .groovy file is present there. Sometimes if you have a .groovy in a source directory, your IDE assumes this is 'source' code and compiles it down to bytecode. The result is you would end up with a .class file on the classpath instead of the groovy script itself. This will vary with settings and what IDE you are using. If this seems to be the problem there may be an option for this or you can use an alternate extension that will not be picked up by the compiler.

Resources