You will learn to create a simple spark API in maven. Spark – A micro-framework for creating web applications in Kotlin and Java 8 with minimal effort. Make sure maven is installed by running mvn --version in the terminal/command prompt. I will build a website registry app, which stores websites information in a HashMap (in-memory database). This is an example app, used in later references.

Steps to create a Spark app in Maven

Step – 1
Download the tutorial content from above link, extract the zip. You will see spark-api-example . This is generated using the below command, but you don’t need to run the command.

mvn -B archetype:generate -DgroupId=com.jstobigdata.maventutorial \
  -DartifactId=spark-api-example \
  -Dpackage=com.jstobigdata.maventutorial \
  -Dversion=1.0-SNAPSHOT

Step – 2
I converted the project spark-api-example to an eclipse project by running the command below. You don’t have to run it.

cd spark-api-example && mvn eclipse:eclipse

Step – 3
Now open the project in Eclipse or STS. You can download these IDE from the respective sites, alternatively, you can also use IntelliJ. Launch the STS/Eclipse, select a workspace, then click OK. After that, right-click on the package explorer, General, Existing projects into Workspace, click next, select the spark-api-example and then next, finish. This will import the project into your workspace.

Step – 4
Run mvn clean install inside the spark-api-example . Now right-click on App.java and run it as Java Application. Now the app is up and running on http://localhost:8090/.

Step – 5
Execute the following cURL commands to make sure sample data gets added.

curl -X GET http://localhost:8090/

Add a few records.

curl -X POST \
  'http://localhost:8090/webSites?domain=jstobigdata.com&siteTitle=Learn%20anything&description=JavaScript%20to%20bigdata'

List websites.

curl -X GET \
  http://localhost:8090/webSites

The idea is to understand how maven is used, how dependencies are added in the pom.xml, and what is the plugin that is added.

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jstobigdata.maventutorial</groupId>
  <artifactId>spark-api-example</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>spark-api-example</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.sparkjava</groupId>
      <artifactId>spark-core</artifactId>
      <version>2.8.0</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.8.8.1</version>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.2</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Keep this handy as I will be referring back to this example again and again.

By |Last Updated: July 31st, 2019|Categories: Java™, Maven|

Table of Contents