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

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 ?

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/

JSPs, static content, SpringBoot jars and IntelliJ

So I'm developing in IntelliJ a spring boot app. Using Gradle, I'm creating the sprint boot jar file.
I'm having problems figuring out where to put the jsps and static content such as .js files in such a way that running the jar AND running from within IntelliJ works!
It seems that in order to get SpringBoot to find jsps in a jar file I need to put the jsps inside a src/main/resources/META-INF/resources directory. For example, META-INF/resources/WEB-INF/index.jsp. I'm pretty sure the WEB-INF is now meaningless.
However, if I try to run this spring boot app from within IntelliJ, it cannot find the jsp. 404, blah blah blah. I actually have to put the jsps in the war-style webapp directory in src/main. However that directory is totally ignored during the spring boot jar build.
So.. how do developers set up their development environments that is both IntelliJ and, say, gradle bootRun friendly?
There is a guide here which should work both in IntelliJ IDEA and in the command line via the war file which can be executed as a jar (via java -jar).
As per spring boot there are Limitations when it comes to jsp's.
To overcome these limitations we need to have the configuration made in the application to render jsp by placing the jsp's under src/main/resources/META-INF/resources/WEB-INF/jsp folder.
Sample Code: Click Here
References:
https://dzone.com/articles/spring-boot-with-jsps-in-executable-jars-1
https://github.com/hengyunabc/spring-boot-fat-jar-jsp-sample

How to create deployable springboot war

I am trying to create a maven spring boot application to be deployed in Tomcat. I am following what is suggested in Spring docs and other stackoverflow suggestions- war, Application.java extending SpringBootServletInitializer, removing spring-boot-maven-plugin from build plugins etc. War file is generated and is deployed in tomcat. But what I found is all static files are packaged under /WEB-INF/classes folder and I am not able to access the page. My project structure is as below:
Can anybody tell me how I can package the war properly to be deployed in Tomcat.
That doesn't change anything.
If you put your static assets in src/main/resources/static (and they end up in WEB-INF/classes/static), Spring Boot will serve them properly. So a src/main/resources/static/foo.png will be available at http://localhost:8080/your-context/foo.png if the context of your webapp is your-context.
Regarding the configuration, you can also go on start.spring.io, click advanced and chose war and you'll get an empty project pre-configured.
Or you can click this: https://start.spring.io/#!packaging=war
The issue is because of version issue. I compiled the application with Java 8 and deployed it in tomcat running under JRE 7. It may help someone facing the same issue.
I got the clue from the below post:
Spring boot war file deploy on Tomcat.

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

deploy web app in jar

I have a problem: I need to create a jar from a web application (I'm using freemarker) and then refer this jar from within a different war.
In practice I need to follow the spring batch admin approach.
How can I do it?
I'm using spring for the first web app.
Many thanks

Resources