serving files from a spring boot war file - spring

I've followed the guide here for turning a "hello, world" level Spring Boot app to a war file. I can run this war like a jar and it will return the simple template.
What I don't understand is why I can't access a main.css file I've created. I've placed it in the resources directory under "static/css/main.css" and according to the docs here Spring Boot will automatically server files under "resources", "static", "public", and "META-INF/resources". However, when I build my war file and run it I can't query those files in the browser (like http://localhost:8080/static/css/main.css). Have a missed a step? If I peek into the created war file I see the "static" directory in "WEBINF/classes" right beside the "templates" directory and the directory holding my application.

Files in src/main/resources/static are served from / so you don't need static in the path. You CSS file should be available from http://localhost:8080/css/main.css

Related

Deploy a embedded Jetty&Jersey war to jetty9,only can see static file?

I cteate a jetty&jersey embedded project with IDEA and Maven,I put it in Github https://github.com/Mengqi777/JettyProject. Run JettyServerStart.java start the server, in the browser address bar enter localhost:8080/dynamic, show OK, enter localhost:8080/static, show static resource file
Now I package it to a war file,and put it in jetty webapps directory. But only can see static file in brower with enter localhost:8080/static.404 Not Found Error when in brower with enter localhost:8080/dynamic
What happend?
What should I do to package this project into a war file or jar file and run it in jetty successfully?
You are doing things in your embedded-jetty usage in JettyServerStart.java that you do not declare/configure in your webapp or war file.
It's a maven project, but not declared as a webapp or war project (in the pom.xml)
Its doesn't have its WEB-INF in the right place (maven directory structure wise), which means your built war file is invalid.
The dependencies are not declared correctly for a webapp or war project (you cannot include jetty-server in a war file)
Your badly located WEB-INF/web.xml does not perform the same configuration steps as your JettyServerStart.java
You don't specify how you created your ${jetty.base} directory to support this war file?
You didn't specify what version of jetty-distribution or jetty-home you downloaded, or are attempting to work with.
The statement "and put it in jetty webapps directory" is unclear, which one? (using jetty-distribution/webapps is invalid and will cause many errors and warnings if you attempt to use it for your own webapps, there's no jetty-home/webapps, and you didn't identify your jetty-base configuration)
The way your project is declared right now, even if manually assembled, skipping maven entirely, you have no servlets, no filters, no listeners, no intializers, only a servlet spec mandated DefaultServlet on url-pattern / giving you static content. That's why accessing http://localhost:8080/static/ works, but nothing else.

Read Spring properties in React

I have a Spring/React webapp. In my application.properties file I defined spring.data.rest.base-path = /apiso when running the app locally, everything is accessible on localhost:8080/api. If I deploy this to my tomcat, the all the stuff goes to localhost:8080/warname/api.
I can easily define my warname in my properties file. And in React,
path: '/api/myStuff'
I can access my data.
Also I can change that to
path: '/warname/api/myStuff'
and everything will work. But to make things easier, it would be better to read the warname from my pom.xml so I wouldn't have to change every path in my .js. How to get that done?
If you have a directory named ROOT in your Tomcat directory, you have to remove it and change the name of your war to ROOT.war so that when Tomcat explodes the war it will be the main root project.

Springboot application cannot find application.yml from external folder (#PropertySource)

I am trying to write a spring boot app (for tomcat), which has an application.yml file for configuration.
My goal is to search for this yml file FIRST from the resources, and if it is not there, then SECONDLY from the tomcat's conf folder.
My code, which is not working:
#SpringBootApplication
#ComponentScan(basePackages = "com.my.app")
#EnableConfigurationProperties
#PropertySource(ignoreResourceNotFound=true,value={"classpath:application.yml","file:${catalina.base}/conf/application.yml"})
public class MyApplication extends SpringBootServletInitializer {
...
}
I want this, because if I work on my Eclipse, and want to use Tomcat server from Eclipse. In this case I want to use the resources folder. But if I build the war then I skip the yml file from it with the maven war plugin, and deploy it manually to Tomcat, I want to use the tomcat's folder.
if I leave the yml file on the resources folder of the project, it works
if I delete the yml file from the resources, and place to the ${catalina.base}/conf folder (which is the embedded tomcat for eclipse, I know it), it doesn't work
if I delete the #PropertySource annotation, and leave the yml file in resources, it works
So my app works only if the yml file on the resources folder, even if I add or remove the #PropertySource annotation.
How should I set to search for the resources folder for first time, and if there is no yml file, then search for the second option, which is the tomcat folder?

How do I add a directory in /target to the resulting JAR with Spring Boot?

I'm using Enunciate to generate REST documentation upon building a REST application. Enunicate outputs documentation to /target/docs. I would like to add the /docs directory to the resulting JAR file (and rename it) to be able to serve docs as static content.
How do I do this? I can't figure out how to get these static files (which are generated upon build) into the JAR.
I guess you can solve this by configuring the Maven plugin for enunciate and wiring it up to be run in the 'generate-resources' lifecycle phase.
Also, make sure you set the output-dir to a subdirectory of src/main/resources/static, as commented by Rob above.
I added this to my enunciate.xml to force the docs directory to be generated in a custom location which will be packaged with the .war file
<docs docsDir="target/<app_name>/docs"/>
and then maven will put the entire contents of target/ into the resulting war file package

ClassNotFoundException on Tomcat Server

I am using this tutorial to set up Tomcat Server. After I have put the HelloServlet.java in classes and Web.xml in the WEB-INF folder and I'm giving the command
localhost:9999/hello/sayhello
On the browser. I'm always getting ClassNotFoundException. If anyone can tell me where am I going wrong.
I'm using JDK1.6.0_30, and Tomcat7 for my sample application.
You need to put the compiled HelloServlet.class file (not the .java file) in the WEB-INF/classes folder.
Compile HelloServlet.java and place the output class file HelloServlet.class into
<TOMCAT_HOME>\webapps\hello\WEB-INF\classes\HelloServlet.class
One thing you should check is that, the HelloServlet.class should be in a package. Looks like you have a long way to go with Servlet and JSP... I recommend Head First Servlet and JSP for your reference.
In your 'WEB-INF' of 'classes' folder place the .class files and if your using JDBC, jsp's just Copy the .jar executable files into 'lib' folder. and make sure that xml file should contain the proper information.
you should follow below Web Application Directory Structure
WEB-INF/ --
web.xml --xml file
classes/ ---classes folder here we keep .class files Myservlet.class
lib/ ---lib folder here we keep all .jar files. Myapp.jar
Welcome.html
Welcome.jsp

Resources