Defining context path for spring boot application in external tomcat - spring-boot

I have a spring boot application abc.myapp.war, that is deployed on external tomcat server. By default the tomcat uses the war file name as the context path of the application deployed so it becomes http://localhost:8080/abc.myapp, but I want to have a custom context path like http://localhost:8080/abc/myapp.
I have read through other post they suggest using context tag in server.xml of tomcat, but there is not clear mention how to use it. Can anyone suggest any reference or way to change the context path of the application.
Other ways of doing this are also welcome.

If you want your app to appear with a context path of /abc/myapp you don't have to write any context file, just rename the WAR file to abc#myapp.war: the # will be converted to / (cf. Tomcat documentation for more examples).

Related

Does every spring boot application create a tomcat container to be able to run?

For example,
I understand adding specific dependencies like spring-boot-starter-web that contains tomcat as transitive dependency triggers the spring framework to initialize tomcat container but I want to know if we say a spring-boot application is running, does it imply always that tomcat is also running?
I want to know if we say a spring-boot application is running, does it imply always that tomcat is also running?
No, it doesn't.
Spring boot can be used for both web applications and non-web applications.
For web applications you can use tomcat/jetty/undertow/netty for example, its up to you to chose what works for you the best.
If you don't want to run an embedded version of the web server you can opt for creating a WAR file and place it into the web server prepared in-advance.
If you don't want to run web application at all (something that is built around "Request - Response" way of work in the most broad sense) - you can create a "CommandLineRunner" - in this case you don't need to depend on neither web-mvc nor webflux. For more information about Command Line Runners read here for example
You can have an embedded server in your JAR that is able to be run on its own. By default it is Tomcat but you can change this to others, like Jetty. Additional details:
https://docs.spring.io/spring-boot/docs/2.0.6.RELEASE/reference/html/howto-embedded-web-servers.html
If you don't want to have an embedded server and you want to deploy your application you can also create WAR files instead of JAR files. Additional details:
https://www.baeldung.com/spring-boot-no-web-server
https://spring.io/guides/gs/convert-jar-to-war/

embedded tomcat setenv configuration with spring boot

I want to provide the contents of setenv when running a spring boot app on an embedded tomcat container. What is the best place to do this?
I would like it to be in an external file and also ensure that tomcat is able to access it before the spring context loading kicks in.

How to Get Spring Boot Application Root Path?

Spring Boot Applications Use Embedded Tomcat/Jetty So How to Get Spring Boot Application Root Path?
In Spring MVC applications we deploy our application to external tomcat contianer so we can use HttpServletRequest class to find root folder path.
But in Spring Boot application using with HttpServletRequest does not return application root folder path. Spring Boot application is java application with main method.
So I use this Workaround. If you find any better way, share with me. thanks.
YourClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
CodeSource.getLocation() Returns the URL location associated with code source.
URL.getPath() Gets the path part of URL.

Moving Spring boot web application in tomcat 8

I made a little web app with Spring Boot with web MVC, it is working fine if I run it by deploying the war or if I run it directly from STS. The problem is that my application is always run with his appname as context app (something like "localhost:8080/appname/") and I can't change it. I tried to write a web.xml with dispater-config.xml but, even if the server recognize it, the path is not changed. I tried to write the method in the SpringBootServletInitializer and setting the path, but it is not working too. I also tried to add a META-INF/context.xml in the webapp folder trought STS (project->src->webapp->META-INF->context.xml) but it's not working too. I am just going crazy with, what should i do to change the app's context path? Thanks
EDIT: I would mount my app in the root context of tomcat
It's built in, in eclipse (STS). I suggest using a stand alone tomcat and not the integrated eclipse tomcat and deploy the war on your stand alone tomcat
Add the context path as a parameter to your #RequestMapping annotation, like this: #RequestMapping("/helloworld")

Where can i store(keep) xml files of a java application inside the grails application

I am using spring batch processing code of a java application in my Grails application. I wanted to know where can i keep the xml files of a java application inside the grails application. How can i call that xml file from the grails application?
There is a special place for such config files:
grails-app/conf/
Put it here, and file will be accessible from classpath, as a root element

Resources