Where is the work directory of embedded tomcat for a spring boot / spring mvc application? - spring-boot

I would like to ask where is the location where you can see the work directory of a spring boot / spring mvc applicaiton. Specifically I want to know Transpiled JSP files converted to java files.
I can see the class files inside the target directory, but cannot find the transpiled jsp files. Is there a class call where I can see it in my environment? Like System.getProperty("user.dir") or new java.io.File(".").getCanonicalPath(). I tried both and it only showed me the code path.
Also, I know the Eclipse Server path. But in spring boot, it does not seem to run using a server instance of tomcat. So the following path is not existing?
The following directory is also blank.
projectworkspace/.metadata/.plugins/org.eclipse.wst.server.core/

The base dir is set via the property server.tomcat.basedir, see: https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html
The work dir is derrived from there by appending /work.

Related

Internal Magic of a multipart file upload Springboot

I have spend lots of time to understand the internal working of a multipart file upload in spring boot.
Couldn't get a clear picture on it.
Bit confused about the role of spring boot tmp directory.
I have a tmp directory named as /tmp/tomcat.4296537502689403143.8587/work/Tomcat/localhost/ROOT]
I was checked the tmp directory during the file upload, couldn't write anything here.If i delete the folder it will throw multipart error.
Can anyone explain the internal working of a file upload and the role of tmp directory.
Spring boot web framework comes with embedded web servers: Tomcat by default. Tomcat creates/uses the tmp directory to store temporary files; including uploaded files, session files, and other files.
That behavior can be changed through configuration. Alternatively, you can also configure spring boot to use a different web server.
https://github.com/spring-projects/spring-boot/blob/70eee612ff2a2b1e58cbcb18a4d46e464895c18a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java

How to specify Log4j2 configuration file in spring boot application

I am using log4j2 in my spring boot application. This works in all respects re: excluding slf4j, including log4j2, etc.
But when the application deploys I need to customize the file for each target host. I have created an ansible role that does this. Ultimately I end up with a log4j2.xml file deployed in another directory e.g. /prod/produsrX/data/log4j2.xml.
I am using the spring-boot-maven-plugin "repackage" goal to generate an executable jar file. It doesn't seem like that should matter but it is a data point in the problem.
This was supposed to be the easiest part of the project. Always before I have just been able to set -Dlog4j.configurationFile - advice which is echoed on about 3,000 web pages and DOES NOT WORK in Spring Boot 2.1.3.
The most useful info I've found is this question. It talks about using -Dlogging.config because logging must be initialized before other properties are read. Unfortunately that didn't help either.
I did find one example that suggested specifying the above directory in a -classpath parameter to java. But that didn't help either.
Does anyone know how to get a spring boot application to read the log4j2.xml file?
The property actually has to be put into the application context (e.g. application.yml). Using a -D property does not work!
logging:
config: /prod/produsrX/data/log4j2.xml #fully qualified name to your log4j.xml

Spring boot 404 error if the static content is deployed as jar

I have spring boot application and angular js project as two different modules.
The angular js contents ( files inside 'dist' folder) are converted as jar files and included as part of Spring boot application. The folder structure inside the jar is /static/**.
The jar appears inside the lib folder of the spring boot application.
Now when i try to access the application http://localhost:8080, i get 404 error.
It works fine, if i copy the "dist" folder content inside the /resources/static and create spring boot jar file.
Am i missing anything when the static contents are included as jar file. ?
Thanks in advance
I have a similar set up like you but using Angular 4. My Spring Boot version is 1.5.3.RELEASE.
My build is based on Maven with two modules. One for front-end (JAR) and one for backend (WAR), which references first module with Maven dependency mechanics.
Providing static content works out of the box. Since I'm using Spring Security I have to configure access rules for this resources. But this issue should be answered with a 401 and not 404.
Your angular JAR structure seems to be correct. Is your front-end build complete?
The lib folder with the front-end JAR is in my Spring Boot WAR on path /WEB-INF/.
Did you check if there is a bug connected with your used Spring Boot version?
#Moshe Arad #Raj In my understanding from https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot ,
the static contents can be in one of those mentioned locations. However, I myself is facing struggle in serving static content. I was working on spring-boot and angular-cli(6.1.5) based app. For deploy purpose, so that UI works on spring-boot port; I ran ng build. It generates files in src/main/resource/static (changed outputPath in angular.json to resources/static and create an empty dir static in resources) . But spring-boot didn't serve any angular content at 8080. Although index.html has <base href="/">, I thought to be more specific for it to pick/serve .js contents from static dir so I ran :
ng build --base-href / --deploy-url /static/ . This time the generated index.html has /static/runtime.js etc. in . But when accessing localhost:8080/ (right click -> inspect shows 200 response code) still doesn't show any UI content. But hitting localhost:8080/static/runtime.js shows 404 .
I believe there is more to do beyond just adding content to resources/static as probably tomcat is trying to serve it but somehow the content not being displayed. Because I do notice favicon icon (angular icon) on browser tab and the contents of jar file shows generated static files in the path (BOOT-INF/classes/static/index.html etc.)

Reload static content spring boot application

I am using Netbeans and I am developing my first web application using spring boot. I was keeping my HTML, js, CSS in "webapp" folder and then I refactored my project and I put all static content in /resources/static. Since then, I have to rebuild my project every time because the static content isn't reloaded.
Can I easily bypass this problem if I'll use browser-sync plugin for Gulp?
Add the following to src/main/resources/application.properties:
spring.web.resources.static-locations[0]=file:src/main/resources/static/
spring.web.resources.static-locations[1]=classpath:/static/
The "file:" causes the content to be reloaded on refreshing the browser,
see related issue.
Alternatively, the file resource locations can be discovered at runtime and added programmatically.
See also documentation and tutorial.
Note that prior to Spring Boot 2.4, the property was called "spring.resources.static-locations".
Normally the static content is copied to the build directory ( target if you are using maven) by the spring-boot plugin. You can find your files at {build-directory}/classes/static: These are the files that you should modify to reflect changes. You should also copy your changes to resources/static, because whenever you restart spring boot, the files are copied.
if an application.yml file is used for configuration, insert:
spring:
web:
resources:
static-locations[0]: "file:src/main/resources/static/"
static-locations[1]: "classpath:/static/"

Embedded Tomcat, executable jar, ServletContext.getRealPath()

I'm using a standard spring boot approach (a Java project) where we use embedded Tomcat and package as an executable jar.
I place some static resoures e.g index.html in a static folder under src/main/resources of my project. A library we use expects to load some resources using the servlet context by doing servletContext.getRealPath("XXXX").
When I run this project in Eclipse the real path for servletContext.getRealPath("/") resolves to C:\Users\<me>\workspace\springboot-project\src\main\webapp\. I have no idea why it uses this folder when it does not exist in the project? If I create the executable jar and run this then servletContext.getRealPath("/") gives C:\Users\<me>\AppData\Local\Temp\tomcat-docbase.5693809153072003983.8080\. This directory does exist but has nothing in it.
I can obviously see my resources fine if requested via HTTP I just can't get at them as expected via the servlet context.
Any ideas?
Resources in src/main/resources/static aren't served directly by Tomcat. They're served by Spring through its DispatcherServlet and a ResourceHttpRequestHandler that's auto-configured by Spring Boot. By default, this resource handler is configured to serve resources from the following locations:
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
It's 3 that's making your resources in src/main/resources/static available.
Using getRealPath() is always going to be problematic as it requires the resource to be available as a file on the filesystem, i.e. it can't be inside the war or jar that you're running, and there's no guarantee that that will be the case. Using ClassLoader.getResource() or ServletContext.getResource() is a much more robust way to find and load resources within your web application as it doesn't rely on the resource being directly on the filesystem.
You haven't said which library it is that's using getRealPath(). Perhaps it can be updated or be configured to use a different resource loading strategy?

Resources