A lightweight web application built with SparkJava, demonstrating REST API routing, JSON serialization, Mustache templating, and static file serving — all on an embedded Jetty server.
| Layer | Library | Version |
|---|---|---|
| Web framework | spark-core | 2.7.2 |
| Template engine | spark-template-mustache | 2.7.1 |
| JSON | Gson | 2.8.5 |
| Logging | slf4j-simple | 1.7.21 |
| Build tool | Gradle | — |
- Java 8+
- Gradle (or use the included wrapper)
./gradlew run # Linux / macOS
gradlew.bat run # WindowsThe server starts on port 8080.
./gradlew build./gradlew test
# Single test class
./gradlew test --tests "com.alexxrw.sparkjapp.YourTestClass"Returns a plain-text greeting.
GET http://localhost:8080/
→ Hello World
Accepts a JSON body and query parameters, returns a JSON object.
Query parameters
| Param | Description |
|---|---|
device |
Device type |
user[age] |
User age |
user[gender] |
User gender |
Request body
{ "mood": "joy" }Example
// From the browser console:
await (await fetch('/Ben?device=notebook&user[age]=16&user[gender]=male', {
method: 'POST',
body: JSON.stringify({ mood: 'joy' })
})).json()Response
{
"name": "Ben",
"device": "notebook",
"user-age": "16",
"user-gender": "male",
"mood": "joy"
}Returns an HTML page rendered with the Mustache template (templates/index.html).
src/main/
├── java/com/alexxrw/sparkjapp/
│ └── Starter.java # All routes, JsonTransformer, MoodHolder
└── resources/
├── public/
│ └── index.js # Static assets served at /
└── templates/
└── index.html # Mustache template
- All responses are gzip-encoded via an
afterfilter. - Unmatched routes return
{ "Error": "Wrong data" }as JSON. - The SparkJava debug screen is enabled in development — accessible at
http://localhost:8080/__debug.