Spark rest api server deploy

痞子三分冷 提交于 2020-01-24 09:56:46

问题


With this link, I made rest api server. It works well when I run on eclipse ide. However, I don't know how to deploy on server. I made war file and tried to deploy on tomcat, but somehow cannot access any page that I defined, unlike running on eclipse. Here is some gradle configuration that I made.

apply plugin: 'war'

war {
    baseName = 'server'
    manifest {
        attributes 'Implementation-Title': 'SparkAPIServer', 'Implementation-Version': '1.0', 'Main-Class': 'com.server.Main'
    }
}

I'm sure that 'Main-Class' path is correct. Any idea of this?


回答1:


Running from Eclipse IDE is not the same like running on Tomcat, because when running on Eclipse Spark uses the built-in Jetty server, and when deployed to Tomcat Spark runs on Tomcat server.

Quoting from the documentation:

Other web server

To run Spark on a web server (instead of the embedded jetty server), an implementation of the interface spark.servlet.SparkApplication is needed. You have to initialize the routes in the init() method, and the following filter has to be configured in your web.xml:

...

So in order to run the same code after deployed to Tomcat, you need to:

  1. Your class should implement SparkApplication.
  2. Implement the init() method, and register all your routes there. (Of course, if you want to be able to run both locally on Eclipse and remotely on Tomcat, just register all your routes to some private method startSpark() which will be called both from init() and from main()).
  3. Your web.xml should be set accordingly.



回答2:


Seeing that you use Spark and a main method to start a webserver, you are looking actually to create a jar, like the following (adapt as needed):

jar {
  manifest {
    attributes 'Main-Class': 'com.foo.bar.MainClass'
  }
}

See the jar task documentation. And don't forget to add the classpath for your external libraries into the manifest.



来源:https://stackoverflow.com/questions/42690106/spark-rest-api-server-deploy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!