How to Get Spring Boot Application Root Path? - spring

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.

Related

Defining context path for spring boot application in external tomcat

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).

Springboot finds files in src/main/webapp but not src/main/webapp/anything

I'm using Sprint boot to run a web application. I did not configure any classes explicitly. I have a strange problem. When running the web server using gradle bootRun, I am able to access files that are directly under webapp folder but not a sub-folder like webapp/something.
I'm not posting any code here because there is nothing explicitly I configured different from a default spring boot web app. I'm using structure as in https://github.com/jhipster/jhipster-sample-app-gradle
Anyone faced this problem before ?

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")

Should I use `src/main/webapp` to serve static content with Spring Boot?

The documentation of Spring Boot states:
Do not use the src/main/webapp directory if your application will be
packaged as a jar.
But surprisingly the Spring Boot Sample for static web files is using the /src/main/webapp directory. And also JHipster is using the webapp folder.
So I'm confused. Is the warning in the documentation of Spring Boot outdated? Is it now considered good practice to use src/main/webapp to serve static files with Spring Boot jar applications? And if not, what is the recommended practice now when using Spring Boot in a Maven setup?
From Spring Boot Reference Documentation (emphasis mine):
By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext. It uses the ResourceHttpRequestHandler from Spring MVC so you can modify that behavior by adding your own WebMvcConfigurerAdapter and overriding the addResourceHandlers method.
In some of my (Maven) projects I am currently using src/main/resources/static because it's considered part of the classpath by default and IDEs (like Eclipse) tend to like this.
Be wary because Spring Boot 2.4.x disables the default servlet (which impacts on loading of src\main\webapp and anything else at the root of your WAR file. JAR file packaging is disabled by default but there is a configuration option to all that too - see Spring boot: configure it to find the webapp folder.
https://github.com/spring-projects/spring-boot/issues/22915
The full release notes are here: https://spring.io/blog/2020/11/12/spring-boot-2-4-0-available-now
hi if i understand your question
when your are using jar packaging in spring boot
yo will put your resource in META-INF/resources
and you can use this jar package in other project and call resource ,
you can move webapp to meta-inf directory using maven and gradle build

How to configure wily with spring boot application

I have created an application as spring boot application,Now i want to integrate this application with wily,where i ll passed wily parameter in application to configure application.
The fact that it's a Spring Boot application shouldn't make any difference. If you're using an executable jar file, you'll need to configure the agent when you launch the jar, for example:
java -jar my-app.jar -javaagent:<Agent_Home>/Agent.jar -Dcom.wily.introscope.agentProfile=<Path_To_Agent_Profile>
If you're deploying your Spring Boot application as a war file to a servlet container or application server, you'll need to be make the equivalent configuration changes. The documentation describes how to configure Tomcat.

Resources