How to create a Spring web app on a server - java

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

Related

Spring boot can not find jsp views

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

Gradle jettyRun set webappsourcedirectory with DropWizard

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

Deploying war file created using jersey api

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.

What's the simplest way to deploy a Grails-generated WAR file?

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.

Create navigation menu dynamicly based on deployed jars

I am just investigating the idea of this so have no example code. I've been digging around on the web but I'm not really sure what I should actually be looking for so I'm not finding much. So I thought I'd ask and see if anyone knew if a) This is possible. b) How I should do it. (Or at least what I should be looking to learn about to do it.)
I'm building a web app using JSP pages on the client with a JBoss server running J2EE, in the middle there is a tomcat web server.
Basically the app contains different sections that will be rolled out over time as they're developed and also customers may be using different combinations of sections. The tidiest way of deploying this I can think of is to build each section into it's own jar. Then depending on the combination of sections that are relevant for the customer, install only the required jars on the JBoss server for deployment.
To support this I'd like the client navigation menu to only show the available sections based on what is deployed on the JBoss server. Is it possible for my servlet to find out what is deployed on the server? I'd like the solution to be as 'dumb' as possible, i.e. I don't want to tell it what to look for, (other than a prefix to identify our jars), as I don't know yet everything we might build.
My current solution is to have a table in the database to hold a list of the installed sections. But this is going to require update scripts etc during install and I'm sure we should be able to do this by just deploying jars on the server.
Thanks in advance.
You could add this information to the MANIFEST.MF file and then enumerate all files in your webapp (see this answer for how to list all manifests) when you start.
I do something similar by configuring a "Plugin Directory" setting for my application. It then scans that directory regularly for Jars. It looks for specific metadata in the manifest to determine whether the Jar is actually a valid plugin, and what class to load from it (the static initializer of that class registers the plugin with the application).
Then all you need to do is place a new Jar into that directory to add its functionality.

Categories