Recently, my team decided to use
Spark to build our new REST api. Spark seems easy with less coding. We still wanted to use
Spring for dependency injection. We also wanted to use Tomcat as the standalone web container. After some research, we figured out how to build the REST api with Spring, Spark and Tomcat.
- Create class SpringSparkFilter which extends SparkFilter. Override method getApplication:
-
Create class MySparkApplication, which implements SparkApplication. Implement init method:
-
Configure web.xml to use SpringSparkFilter:
Then either build a war file, and deploy it to tomcat or use
tomcat7-maven-plugin for test run. I usually use tomcat7-maven-plugin for fast development. Here is the command:
mvn tomcat7:run
Then access http://localhost:8080/current-date. The complete example can be found
here.