MichaelP MichaelP - 1 month ago 8
Java Question

How to deploy a spark Java web app?

I used spark web framework to create a webapp, but I don't know how to deploy this webapp. I'm sorry if this is very basic, but I'm new to spark framework and I cannot find any document that guide me how to deploy a spark webapp.:


  • How to deploy a spark webapp standalone

  • How to build a spark webapp (to war file or such a file) and deploy with web server (jetty or Tomcat).


Answer

You will first need to create a regular Java project that can be built into a .war file (in Eclipse this would be a Dynamic Web Project)

The spark documentation at this link describes what needs to be added to your projects web.xml file. http://sparkjava.com/documentation.html#other-webserver

the param-value listed in the documentation within the filter needs to point to the class where you have defined your routes.

Additionally, all the code that was previously in main() needs to be moved to init().

@Override
public void init() {
    get(new Route("/test") {
        @Override
        public Object handle(Request request, Response response) {
            return "response goes here;
        }

    });

Also, in order for me to deploy it to JBoss, I had to only include the spark libraries and not the Jetty libraries. Once this was done, you should be able to build the war and deploy it to your server the same way you would any other Java project.