Build a REST API with Java's built-in HttpServer - HTTP basics, request handling, JSON serialization, routing, and exposing the task manager over HTTP.
Building a REST API
In the previous chapter, the task manager ran from the command line. Now we will expose it over HTTP so any client - a
browser, a mobile app, or curl - can interact with it. We will use Java's built-in
com.sun.net.httpserver.HttpServer, requiring zero external dependencies.
HTTP and REST basics
HTTP is the protocol the web runs on. A client sends a request, the server sends a response.
A request has:
- Method:
GET,POST,PUT,DELETE - Path:
/api/tasks,/api/tasks/1 - Headers: metadata (content type, auth tokens)
- Body: data (for
POST/PUT)
A response has:
- Status code:
200 OK,201 Created,404 Not Found,400 Bad Request - Headers: metadata (content type, cache control)
- Body: the data (usually JSON)
REST (Representational State Transfer) is a convention for designing APIs around resources:
Project structure
We will extend the task manager from the previous chapter:
Copy Task.java and TaskStore.java from the previous chapter. We will add three new files.
Step 1: JSON helpers
Since we are not using any libraries, we need simple helper methods to convert tasks to/from JSON. For our small data model, manual JSON is straightforward:
This is intentionally minimal - it handles our specific data format. For production APIs, use a library like Jackson or Gson. See the JSON Processing guide for library-based approaches.
Step 2: the request handler
The handler processes HTTP requests and routes them to the right action:
Key points:
HttpHandleris the interface fromcom.sun.net.httpserver- implementhandle(HttpExchange)- Routing is manual - check the path and method to determine the action
- Responses are always JSON with the appropriate status code
- Errors are caught and returned as JSON error responses
Step 3: the server
The server:
- Listens on port 8080
- Routes all
/api/*requests toTaskHandler - Prints the available endpoints on startup
Step 4: compile and run
Result:
Step 5: test with curl
Open a new terminal and test each endpoint:
Health check
Result:
Create tasks
Result:
Result:
List all tasks
Result:
Get a single task
Result:
Complete a task
Result:
Delete a task
Result:
Error handling
Result:
Result:
Step 6: package as a JAR
Create MANIFEST.MF:
Build:
Run:
The JAR is self-contained - it uses only built-in Java libraries, so it runs anywhere Java is installed.
What you have built
The API follows REST conventions with proper:
- HTTP methods (
GET,POST,PUT,DELETE) - Status codes (
200,201,400,404,405,500) - JSON content type headers
- Error responses in a consistent format
For building more complex APIs, you would typically use a framework. See the HTTP Clients guide for how to consume HTTP APIs from Java.
Summary
- Java's built-in
HttpServerprovides a zero-dependency HTTP server. - A
HttpHandlerprocesses each request - check the method and path to route. - Send JSON responses with
Content-Type: application/jsonand appropriate status codes. - Manual JSON works for simple models - use Jackson or Gson for complex data.
- The same
TaskandTaskStoreclasses from the CLI project power the API. - Package as a JAR for easy distribution.
Next up: Deploying to a VPS with Nginx - putting your API on the internet.