Helo everyone!
I completed my Spring based web app and then started to rebuild it to Spring boot because it gives me ability to use embedded jetty.
At the moment I got some problem - Spring boot can not find (resolve) jsp views. So when I run my app - I get the error in browser:
Here is my web app on github - so you can just look at files and find the reason.
Help me please!
P.S. Yes I used #ResourceImport because I don't know how to rewrite the existing spring configuration xml files to java-based config files but you may show me HOW to do that in your answer.
P.P.S. And for some reason the target directory does not contain WEB-INF and it's content folders after compiling. May be this is the reason but I don,t know how to solve it!
I checkout-ed your code from github and managed to deploy into embedded Tomcat 8.0.3 and standalone Tomcat 8.0.x.
I will just go through the steps for embedded Tomcat instead of Jetty(as I have not figured out the Jetty configuration fully yet).
There are two things that you could do
Copy your webapps into the following location without the s, e.g. /dvdexchange-spring-boot/src/main/webapp
Modify slightly your pom.xml, e.g. below
The final outcome as below
-
UPDATE:
IngeniousTom,
I was not able to make it work in embedded Jetty, the furthest point I could reach after struggling to add numerous Maven Jetty jar libraries as shown below
This is actually a known issue and I do not see how this can be solved without any hackish way.
If you read in the github link, there is numerous discussion between Spring-Boot and Jetty camps.
The bottom line of the discussion is that Spring-Boot does not support yet Jsp in embedded Jetty as their standard but have plans in future.
My recommendation is not to use Jsp or use other than Jetty as your embedded container.
JSPs in executable JARs are not officially supported by Spring Boot, this is one of the known JSP limitations. Also, Spring Boot supports JSPs in embedded Jetty as of 1.4.0.RC1, not before.
Right now you can solve this by using Spring Boot 1.4+ and packaging your application as an executable WAR. You'll be able to deploy your app as a regular WAR, run it with java -jar app.war and have a nice development experience - all of this with standard JSPs.
Maybe you'll find tricks to work around those limitations, but keep in mind that those often rely on container-specific behavior that aren't supported by the servlet spec. So at best, this behavior will be inconsistent between containers - and it's certainly possible that containers may change their behavior at any time.
If you really want to package your app as an executable JAR, then the best approach is not to use JSPs and pick a proper template engine.
If you want to do a jar-Deployment you cannot use the wepapp folder.
Put your JSPs to src/main/resources/META-INF/resources/jsp
Put these lines to your application.properties file:
spring.mvc.view.prefix=/jsp/
spring.mvc.view.suffix=.jsp
You can have a look to this project. It also uses Spring-Boot, a jar-Deployment and JSPs: https://github.com/synyx/urlaubsverwaltung
Related
Firstly, please excuse my horrendously general question, as my understanding of Spring is very limited, but I will expalin what I want to achieve, and hopefully someone can point me in the right direction.
I have an application that retrieves some information from some source and updates a database. I'd like to put this program on a Tomcat server, so that the application is run every day.
I'm very new to Spring, and have spent the last few days completing some basic tutorials to display Hello World! in a broswer.
However, all of the tutorials I have found relate to Controllers for URLs, which, as far as I understand, is not what I want, as my application will not have a URL and there will be nothing to display, I just want the application to "hidden" somewhere on the server, and to execute daily.
I know this is a very general question, and as I said my knowledge of Spring is next to non-existent, so I'd appreciate it if someone could point me in the right direction, I'll happily do research if I just knew what to look for.
Thanks in advance!
I would suggest using Spring Boot quite easy to start with and does all the magic for you.
There are few tutorials how to start and what you will need.
You can have a jar that will run on embedded Tomcat server provided by spring boot, or you can convert it then to a war/ear file and deploy it on External Tomcat if you wish (doesn't need to be tomcat either). You just need a deployable artefact. In this case a war or ear.
Difrence between jar, war, ear
if you then wish to convert it:
Convert a jar into a war
Deploying Spring boot apps on External servers
All the documentation you can find on Spring guides
I'd like to put this program on a Tomcat server, so that the application is run every day.
Seems to me that you don't really need Tomcat or Spring.
Why not just install your Java app on a UNIX server and have it run every day with cron ?
Spring is a very frustrating framework to learn because of all the various versions and tools out there.
Spring Boot is usually the starting point for people since it creates an executable self-contained JAR with embeded Tomcat server.
If you want to run with your own Tomcat instance, you need to look into creating a WAR/EAR file, which is an archive with a directory named WEB-INF that contains all of your Tomcat XML configurations.
Turn your Jar into a War
One of the simplest ways to start is to use Maven, add the WAR plugin to your pom.xml file, then webapp/WEB-INF directory to your project and place the web.xml config file in it.
Create and deploy web apps on Tomcat
So I'm developing a DropWizard application and all of the tutorials point towards compiling and running java -jar to start the web server. However while I'm doing local development this is a pretty slow work flow. Having used Jetty before I know it will autoreload and run in a daemon mode.
We're using Gradle and I found this which works to start Jetty. The first problem I encountered is this:
Directory '/src/main/webapp' specified for property 'webAppSourceDirectory' does not exist.
I found I way around this by adding
jettyRun.webAppSourceDirectory = file("src/main/java")
to the build.gradle file but of course this just lists files in that directory. Is there a directory I can point jetty to for this to work?
Or is there another way I can get DropWizard to auto reload resources and recompile?
Also Is there a way to get DropWizard to run in the background?
Dropwizard doesn't run on Jetty. It manages Jetty, as well as other tools. So manipulating jetty is not a solution for what you want to accomplish.
Or is there another way I can get DropWizard to auto reload resources
and recompile?
No AFAIK.
Also Is there a way to get DropWizard to run in the background?
Also no AFAIK. You should be able to fix that with some bash tricks.
Or maybe this might be of some help, but I don't think it will recompile and reload resources.
Dropwizard is a fairly lightweight application. In my development environment it takes about 3-5 seconds to build and start a dropwizard service; that is by using Intellij, not gradle (or maven).
I am completely new to Java ( Beginner Java Developer). Our previous proj was created using Jersey API. I am trying to understand the project as I was not in the team of that project developers.
My Question is.. I have created the War file to that application and deployed it in tomcat. But whenever I click that project from the list, it returns an error (Requested resource is not available). I want know if I can deploy Jersey API web app in Tomcat or not? Also If I can, What may be reasons for that error? I tried everywhere and looked into all other threads but could not understand anything.
Also there is a in web.xml of our project. What is the use of this?
Please help me. Thanks a ton in advance.
I want know if I can deploy Jersey API web app in Tomcat or not?
Yes, you can. You should check your version of Tomcat, and which libraries are required to work with Jersey (usually Jersey jar itself and some common utilities). However, I didnt quite understand "was created using Jersey API". Did you use some Jersey-specific code? Because Jersey is just an implementation of some specification (namely JSR-311).
What may be reasons for that error?
Path is not specified properly, resource not initialized, etc.
Also there is a in web.xml of our project. What is the use of this?
It is a file that is responsible for the settings of 1 application. In your case, your war-archive. In web.xml you can specify the path to your war resources. Nowadays, all resources can be specified with annotations, and no need to modify web.xml exists.
I want to shared common application context in a multi-war Spring application, so i don't need to defined beans again and again. But i don't want to instantiate those beans defined in this common application context in each webapp. I only want to instantiate those beans once and share by all the webapps. Is it possible? Bare me if i'm wrong.
PS: this common application context exists in jar, which i'll import to every webapps.
1, Below article tell us how to share the common application context, but need to instantiate those beans in each webapp.
How to import spring-config.xml of one project into spring-config.xml of another project?
2, Below is another article i just read, i demo it, but still don't get what i want, beans got instantiated twice. Can someone check this section "Why would you want to use this?" in the article, i don't get it, do they have a solution there, can someone help me here, thanks a lot for your time.
http://spring.io/blog/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/
here is the demo source code in second article: https://github.com/jasonluo/ceciic/tree/master/Research/multiple-contexts-sample
Updates
The problem of case2 is because i didn't deploy as a ear file, thanks Deinum for pointing this out, i'm using tomcat, so there is no way to achieve that.
Our solution now is using REST to access the service webapp which running in a separate server.
Don't, there is usually classloader isolation going on to prevent this. Doing this right usually requires a thorough understanding of classloading, Java EE, packaging and your server.
Having that said there is way to do this as outlined in the blog you posted. You have to:
package both WARs into an EAR (this means you have to use and appserver like WildFly AS and can't just use a servlet engine like Tomcat or Jetty)
package both WARs as "skinny" WARs with at least Spring (and all its dependencies) and the shared beans (and all their dependencies) in the lib/ folder of the EAR instead of the WEB-INF/lib folder of the WARs.
Even then it depends on implementation details of the server. AFAIK it is not guaranteed to work under the Java EE specification. For example it likely won't work in GlassFish.
Update
I can't tell if the output of your demo is correct because I couldn't find a build file.
Yes, Tomcat does not support EARs, you could use Apache TomEE which is very similar and supports EARs (I don't know if TomEE does classloading in a way that makes this work). In theory you could also make use of the common classloader in Tomcat but that would be quite a hack.
There is a way to do it, using Spring Dynamic Modules, but it requires OSGi environment, which is quite different from simple Tomcat. Few articles worth reading:
Deploying Spring MVC applications to OSGi
Hello world style example
Blueprint - a further development on DM
Another blueprint documentation reference to Spring
That being said there is not a lot of up to date information about Spring with OSGi, but it's worth a try to achieve just what you said (but of course, with additional performance cost)
I've been having persistent problems (for several weeks) getting Tomcat to deploy a WAR file. I'm looking for a simple server. It does not need to have a lot of functionality-it just needs to be simple to set up. In particular I'm looking for a program that I can just drop a WAR file into and have the enclosed web application launch.
You might want to give jetty runner a try. It basically just uses an embedded jetty instance to run your war file.
http://blogs.webtide.com/janb/entry/jetty_runner
It is available on Maven and it is in fact how heroku apps built with grails are ran. http://devcenter.heroku.com/articles/deploy-a-java-web-application-that-launches-with-jetty-runner
If your application does not start on tomcat, it almost certainly means it won't start on any servlet container - containers implement a spec, and are very similar in many aspects.
What you should do is go and hunt each problem one by one, until the application starts. The problem is the app, not the container.
I recently had a similar problem where my app ran fine with "grails run-app" but not as a war file. It was caused by a missing "package" line in one of the files. I was getting 404 errors. When a war file is unpacked on the server it doesn't put files in the same directory locations as when the are in the grails environment. Not sure this will help you but maybe others with similar problems.
if you get a fresh tomcat instance, generate a hellow world app and generate your war file via grails dev war it should work without a hitch. typically problems arise form adding dependencies that clash with server libs.
if you want to give other servers a try I would suggest resin as one of the easiest to setup, run and maintain.