Skip to main content

ToDo API

The ToDo API provides a RESTful interface for managing to-do items. It demonstrates CRUD operations, JSON serialization, route-based handling, and CORS support.

Base path: /api/todos

info

ToDo items are stored in-memory. Data is lost when the server restarts.

Endpoints

GET /api/todos

Retrieves all to-do items.

Response: 200 OK

[
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"title": "Learn HTTP",
"completed": false
}
]

Example:

curl http://localhost:8080/api/todos

POST /api/todos

Creates a new to-do item. The server generates a UUID for the id field.

Request Body:

{
"title": "Learn HTTP",
"completed": false
}

Response: 201 Created

{
"id": "550e8400-e29b-41d4-a716-446655440000",
"title": "Learn HTTP",
"completed": false
}

Response Headers:

  • Location: /api/todos/{id} — URI of the created resource

Example:

curl -X POST http://localhost:8080/api/todos \
-H "Content-Type: application/json" \
-d '{"title": "Learn HTTP", "completed": false}'

PUT /api/todos/:id

Updates an existing to-do item by its UUID.

Path Parameters:

  • id (UUID) — The to-do item identifier

Request Body:

{
"title": "Learn HTTP/1.1",
"completed": true
}

Response: 200 OK

Error: 404 Not Found if the ID does not exist.

Example:

curl -X PUT http://localhost:8080/api/todos/550e8400-e29b-41d4-a716-446655440000 \
-H "Content-Type: application/json" \
-d '{"title": "Learn HTTP/1.1", "completed": true}'

DELETE /api/todos/:id

Deletes a to-do item by its UUID.

Path Parameters:

  • id (UUID) — The to-do item identifier

Response: 200 OK

Error: 404 Not Found if the ID does not exist.

Example:

curl -X DELETE http://localhost:8080/api/todos/550e8400-e29b-41d4-a716-446655440000

CORS

All ToDo endpoints include the Access-Control-Allow-Origin: * header, allowing cross-origin requests from any domain.

Preflight OPTIONS requests are handled automatically:

EndpointAllowed Methods
OPTIONS /api/todosGET, POST, OPTIONS
OPTIONS /api/todos/:idPUT, DELETE, OPTIONS

Preflight responses include:

  • Access-Control-Allow-Headers: Content-Type
  • Access-Control-Max-Age: 86400 (24 hours)

Data Model

The ToDo record is defined as:

public record ToDo(UUID id, String title, boolean completed) {}
FieldTypeDescription
idUUIDServer-generated unique identifier
titleStringTitle or description of the task
completedbooleanWhether the task is marked as done