Java how to run spark application on apache server

I have a Spark MVC application that is very simple.

According to the spark documentation , this should be enough to run the application:

public class SparkServer { public static void main(String args[]) { Spark.staticFileLocation("src/main/webapp"); System.out .println("bla bla bla"); RService rService = new SparqlRService(); new RController(rService); } } 

I put this class in a package inside my project and I run the web application (dynamic web application) on the Apache Tomcat server.

The print operation does not appear when Apache Tomcat starts, which means that this class is not called. I know that makes sense . that's why i ask. please, how can I let Apache Tomcat launch my spark application?

Update

After @mlk answer , I did the following:

 public class SparkServer implements SparkApplication { @Override public void init() { Spark.staticFileLocation("src/main/webapp"); System.out .println("bla bla lba"); RService rService = new SparqlRService(); new RController(rService); } } 

and in my web.xml I did:

  <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SRecommender</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>SparkFilter</filter-name> <filter-class>spark.servlet.SparkFilter</filter-class> <init-param> <param-name> applicationClass</param-name> <param-value>com.srecommender.SparkServer</param-value> </init-param> </filter> <filter-mapping> <filter-name>SparkFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 

Where is my SparkServer located in the package: com.recommender , which exists in the source folder: src/main/java

I run apache tomcat, but still, when I call any path from the spark, it returns 404.

HITE I can start a spark from the main method and call my pages, they work. so the problem is with the way I configured the spark to run in apache tomcat

Update 2

Here's how to set the path to the view.

 public RecommendationController(RecommendationService service) { get("/", (request, response) -> { Map<String, Object> model = new HashMap<>(); model.put("data", "forza ROMA"); model.put("data2", "It Rome, It home"); return View("src/main/webapp/template/index.html", model); }); 
+5
source share
2 answers

Spark comes with a built-in container, so you don't need a tomcat or jetty. If you want to deploy the container with full deletion, you can create a web.xml file and implement spark.servlet.SparkApplication .

Edit: you are missing the applicationClass property in your Web.xml.

+2
source

WebApps do not execute static void main() methods, they are loaded according to their web.xml from their deployed WAR file.

Do you have any experience with webapps? You need a container like Tomcat or Jetty. The Apache server is for HTTP service only and is not an application container in itself.

+1
source

Source: https://habr.com/ru/post/1243938/


All Articles