Skip to content

Instantly share code, notes, and snippets.

@up1
Last active December 3, 2015 14:56
Show Gist options
  • Save up1/599a9379d47c2f0f648a to your computer and use it in GitHub Desktop.
Save up1/599a9379d47c2f0f648a to your computer and use it in GitHub Desktop.
Demo Spark Java
package up1.demo.api;
import spark.Request;
import spark.Response;
import spark.Route;
import up1.demo.api.model.Employee;
import up1.demo.api.storage.Storage;
public class AddEmployee implements Route {
private Storage storage;
public AddEmployee(Storage storage) {
this.storage = storage;
}
@Override
public Object handle(Request request, Response response) throws Exception {
String firstName = request.queryParams("firstname");
String lastName = request.queryParams("lastname");
Employee employee = new Employee(firstName, lastName);
Long employeeId = storage.addEmployee(employee);
return employeeId;
}
}
package up1.demo.api;
import static spark.Spark.get;
public class APIServer {
public static void main(String[] args) {
get("/", (req, res) -> "Hello World");
}
}
public class APIServer {
private static Storage storage = new InMemoryStorage();
public static void main(String[] args) {
post("/employee", new AddEmployee(storage) );
get("/", (req, res) -> "Hello World");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spark-demo</groupId>
<artifactId>spark-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>up1.demo.api.APIServer</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>demo_api</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment