This project implements a minimal, educational HTTP server in Java.
The goal is to understand low-level request handling, routing, controllers, and response building without relying on large frameworks.
It includes:
- Socket-based HTTP server
- Basic router and mapping system
- Controllers implementing a
RequestHandlerinterface - Request parsing (method, path, headers, body)
- Response builder with support for multiple formats
- A complete class diagram for architecture visibility
The server listens on a configurable port and processes each incoming socket by:
- Parsing the HTTP request into a
Requestobject - Passing it to the
Router - Resolving the appropriate
RequestHandlerviaRoutes - Building the HTTP response with
Responsehelpers - Sending the result back through the socket
The structure is intentionally simple so you can extend it — adding controllers, middleware, or even template rendering.
java-echo-server/
├── README.md
├── src
│ └── main
│ └── java
│ └── com
│ └── server
│ ├── controller
│ │ ├── EchoController.java
│ │ ├── GreetingController.java
│ │ ├── NotFoundController.java
│ │ └── RequestHandler.java
│ ├── http
│ │ ├── HttpMethod.java
│ │ ├── HttpServer.java
│ │ ├── Request.java
│ │ └── Response.java
│ ├── Main.java
│ ├── router
│ │ ├── Router.java
│ │ └── Routes.java
│ └── utils
│ └── ResponseTemplate.java
└── tests
├── load_get_greeting.sh
├── load_post_message.sh
└── run_all_load_tests.sh
Below is a full Mermaid UML diagram representing the architecture:
classDiagram
%% Interfaces
class RequestHandler {
<<interface>>
+handle(request) String
}
%% Enums
class HttpMethod {
<<enumeration>>
GET
POST
PUT
PATCH
DELETE
}
class Response_Type {
<<enumeration>>
TEXT
JSON
}
%% Core classes
class Main {
+main()
}
class HttpServer {
-port : int
+HttpServer(int)
+create(int) HttpServer
+listen(Consumer<Socket>) void
}
class Router {
+handle(Socket) void
+route(Request) String
}
class Routes {
+mapping : Map<String, RequestHandler>
+getOrNotFound(Request) String
}
class Request {
-method : HttpMethod
-path : String
-protocol : String
-headers : Map<String, String>
-reader : BufferedReader
+Request()
+of(Socket) Request
+getReader(Socket) BufferedReader
+setRequestLineFields() void
+parseHeaders() void
+parseBody() String
+hasBody() boolean
+getMethod() HttpMethod
+setMethod(HttpMethod) void
+getPath() String
+setPath(String) void
+getProtocol() String
+setProtocol(String) void
+getHeaders() Map<String,String>
+setHeaders(Map<String,String>) void
+setReader(BufferedReader) void
+toString() String
}
class Response {
+of(String) String
+of(String, Type) String
+echo(Request, Type) String
+bindValues(Type, String) String
}
class ResponseTemplate {
+TEXT : String
+JSON : String
}
class GreetingController {
+handle(Request) String
}
class EchoController {
+handle(Request) String
-valid(Request) boolean
}
class NotFoundController {
+handle(Request) String
}
class IO {
+println(Object) void
}
%% Relationships
RequestHandler <|.. GreetingController
RequestHandler <|.. EchoController
RequestHandler <|.. NotFoundController
Routes "1" *-- "0..*" RequestHandler : mapping
Router --> Request : uses
Router --> Routes : route / getOrNotFound
HttpServer --> Router : passes handler
Main --> HttpServer : creates / listens
Response ..> Response_Type : uses
Response --> ResponseTemplate : uses
Response --> Request : echo()
Request --> HttpMethod : uses
Request "1" *-- "1" BufferedReader : reader
IO <.. Router
IO <.. Request
IO <.. HttpServer
IO <.. Response
%% Notes
Note right of Request : Request.of(socket)\nbuilds a Request\n- getReader(socket)\n- setRequestLineFields()\n- parseHeaders()
Note left of Routes : mapping = Map.of("/greeting" -> GreetingController,\n"/message" -> EchoController")