Spring boot - Deploy war Application in Tomcat Root - spring

I'm implementing a Spring boot application and i have have this requirements:
1 - The generated application must be a war
2 - Deployed as a war in the tomcat root
3 - we don't have to configure anything in tomcat
we are using spring boot 2 and tomcat 9
in our development envrionment we can start the application by the main and as default the spring boot strat it in the root context root \ as requested, this is the standard behaviour of spring boot
instead when we deploy the application in tomcat we are not able to let it start as root.
We know already the work around like renaming the final war ROOT.war, that the application will overwrite the dafault root application in tomcat that has use the \ context root, but this is not an elegant solution, we would like to understand what is the problem
By default if you doplay a spring boot application like a war in tomcat it use the war name as context root : i mean for exampl text.war then the context root will be \test
but if we try to modify the property server.servlet.context-path whatever we put in it
it could be the empty context root that we would like to achieve server.servlet.context-path=\
or another name that doenst mathch with the war name (liek server.servlet.context-path=\test2 ) the application is not properly published in tomcat (404 resource not available)
clearly if instead the war name and the configured server.servlet.context-path match all working properly
we know also the we can configure the server.xml and force the application
to use the context root that we want:
The point that we are not able to understand is
1 - why when we deploy the war in tomcat we cannot configured the server.servlet.context-path becouse if the path configured doesn't match with the war name the app is to reachable
2 - sameone foud a decent solution to automatically deploy the app as context root

Related

Spring boot - Unsupported config data location 'optional:file:./config/*/'

We are migrating a spring boot application from 2.2.4 to 2.4.3
The application can be run with inbuilt tomcat / Tomcat war deployment.
Environment: Windows Azul Java 11 (Zulu 11) and Tomcat 9.0.33
If I run the code with my Netbeans linked tomcat, it works fine.
If I deploy the generated war directly in the same standalone tomcat, it throws the following error.
SEVERE [main] org.apache.catalina.startup.HostConfig.deployWAR Error deploying web application archive [D:\apache-tomcat-9.0.33-without port - rest.war]
java.lang.IllegalStateException: Error starting child
................................................................
................................................................
Caused by: org.springframework.boot.context.config.UnsupportedConfigDataLocationException: Unsupported config data location 'optional:file:./config/*/'
at org.springframework.boot.context.config.ConfigDataLocationResolvers.resolve(ConfigDataLocationResolvers.java:110)
On reading through the Spring document, it was mentioned that By default, Spring Boot includes config/*/ in the default search locations. Ref: https://docs.spring.io/spring-boot/docs/2.4.0-SNAPSHOT/reference/htmlsingle/#boot-features-external-config-files-wildcards
I further debugged with spring boot source and spring core source and identified that there is a mismatch in the classloader that happens that was the cause of this exception.
SpringFactoriesLoader is the class that gets loaded by ParallelWebAppClassLoader in NB linked tomcat and by URLClassLoader in standalone tomcat.
Ref: Line No 129 at https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java#L129
The cache at Line 136 has the config classes loaded by ParallelWebAppClassLoader, but not by the URLlassLoader. So the 2 resolvers - ConfigTreeConfigDataLocationResolver, StandardConfigDataLocationResolver in boot v2.4.3 are not getting identified when it tries to retrieve with the key URLlassLoader (Line 136).
Not sure whether this is an issue with Springboot / SpringCore / Tomcat / any new configuration to be added as a part of our upgrade.
Note: Our application.properties has spring.servlet.multipart related properties only.
"./" is not getting resolved in standalone tomcat.
Try using classpath:application.properties , or classpath:config/* -- this would require props inside war.
or absolute path of file:/var/myapp/config/*. Since tomcat is in D:, you may create a folder d:/var/myapp/config/.
Note that within spring you can ignore "d:" and just mention "/var/myapp/config/*", so, in-case tomcat is in "x:" your code need not change.
This was an issue in Spring boot.
Updating to version 2.4.6 will solve this issue.

Using jasypt-spring-boot when deploying to Apache Tomcat

I'm trying to use the jasypt-spring-boot and deploy it to a Tomcat server as war.
How to pass the encryptor password, in this case, to ensure the encrypted values could be read?
All the provided example are about running a jar file or a Spring Boot app as follows:
java -Djasypt.encryptor.password={my-password-to-decrypt} -jar target/jasypt-spring-boot-demo-0.0.1-SNAPSHOT.jar
May be add some settings to catalina.properties file in the Tomcat conf folder as we do for example when defining active profile?
I figured out how to achieve that:
create a setenv.sh file in CATALINE_HOME/bin folder
add the following entry to set the environment variable on the Tomcat startup:
export JASYPT_ENCRYPTOR_PASSWORD=your-password
save and restart Tomcat.

Tomcat context.xml from META-INF folder for context root configuration

I have an application which is being deployed in Tomcat 8. This application needs to be the context root.
To do this, I put a root.xml file in conf\Catalina\localhost folder and it worked.
Now, the problem is that when i debug through IntelliJ idea (Remote host) it is not being deployed as the root application.
Seems like the only way to fix this problem is to add context.xml file in META-INF folder and specify the configuration there.
Now, when I do this, context root configuration from context.xml file is not being picked up.
Any thoughts on this? Ultimately, I just need to debug the application through a remote server.

TOMCAT 7 not deploying war

I have developed a Dynamic Java Project with Eclipse which involves web services(RESTful). After completion I tried to run my project Tomcat 7 runtime of Eclipse IDE, it works fine. I am able to access all my web services.
But, when I created a WAR file out of my project by right-clicking on project and exporting it to WAR file and placed .war file in Tomcat 7's webapps folder. Started the tomcat by running startup.bat, it throws below exceptions and I am not able t access any web service:
I have placed all the required jar files in lib folder of Tomcat.
Exception fixing docBase for context [/Books] (Books is my war file name)
Failed to create work directory [{CATALINA_HOME}\work\Catalina\localhost\Books]
Failed to create destination directory to copy resources.
Could anyone please let me know what I am missing??
Thanks in advance.
Regards,
Piyush
The failed to create "work directory" error can occur if the work folder does not exist in the tomcat7 folder.
Also this error can be caused by tomcat not having sufficient permissions to read, write and execute in the tomcat7 directory.
The issue is because, tomcat is not finding your web.xml location in the classpath.
Do below steps:
Right click on web project-->Properties-->Deployment Assembly--> Add your classpath to web.xml(src/main/webapp) in my case.

Do i need a context.xml file to deploy spring webapp to tomcat

I am new to Tomcat, so I apologize if this is a dumb question. I have created a spring mvc webapp that currently runs locally using the maven-jetty-plugin.
I can successfully create the WAR file. I would like to deploy the WAR file into a tomcat6 instance. However, I am not sure if I need to create a context.xml file for tomcat? And if I do, where would I place the file in my spring webapp? My current directory structure looks like this:
src
|
|-main
| |-java
|-resource
| |-META-INF
|-webapp
| |-WEB-INF
|-web.xml
The context.xml file is used to configure application specific instructions for your container (tomcat). For instance, you can define JNDI resources, loggers, valves, etc. See Context Container for more details.
By default, Tomcat will auto-generate a default context.xml for your war if you do not specify one yourself and store it within its own configuration files in /conf/[enginename]/[hostname]/[context-root].xml
If you want to include it as part of your war, you can place it in /META-INF/context.xml within your war.
No, you don't. The context.xml file is optional, and by no means required by Spring.
context.xml is used to configure Tomcat itself. The defaults are sensible, there's no need to override them unless you have a good reason.

Resources