Skip to content

Alexxfromgit/SparkJava-application

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

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

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

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"
}

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

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors