Skip to content

Repository files navigation

SparkJava Application

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.

Tech Stack

LayerLibraryVersion
Web frameworkspark-core2.7.2
Template enginespark-template-mustache2.7.1
JSONGson2.8.5
Loggingslf4j-simple1.7.21
Build toolGradle

Getting Started

Prerequisites

  • Java 8+
  • Gradle (or use the included wrapper)

Run the application

./gradlew run # Linux / macOS
gradlew.bat run # Windows

The server starts on port 8080.

Build

./gradlew build

Run tests

./gradlew test# Single test class
./gradlew test --tests "com.alexxrw.sparkjapp.YourTestClass"

API Reference

GET /

Returns a plain-text greeting.

GET http://localhost:8080/
→ Hello World

POST /:name

Accepts a JSON body and query parameters, returns a JSON object.

Query parameters

ParamDescription
deviceDevice type
user[age]User age
user[gender]User gender

Request body

{ "mood": "joy" }

Example

// From the browser console:await(awaitfetch('/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"
}

GET /temp/, GET /temp/everybody, GET /temp/temp/:name

Returns an HTML page rendered with the Mustache template (templates/index.html).

Project Structure

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

Development Notes

  • All responses are gzip-encoded via an after filter.
  • Unmatched routes return { "Error": "Wrong data" } as JSON.
  • The SparkJava debug screen is enabled in development — accessible at http://localhost:8080/__debug.

About

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.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages