Skip to content

Commit 0719216

Browse files
thcedclaude
andcommitted
docs: Update README for Java 25 and post-refactor public API
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a3988e9 commit 0719216

1 file changed

Lines changed: 41 additions & 33 deletions

File tree

README.md

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ It is designed to be simple to use while providing the essential features needed
1919
## Getting Started
2020

2121
### Prerequisites
22-
- Java SDK 21 or later
22+
- Java SDK 25 or later
2323
- A serialization library, e.g. Gson or Jackson
2424
- OpenAPI specification file in JSON format (`openapi.json`)
2525

@@ -29,64 +29,72 @@ It is designed to be simple to use while providing the essential features needed
2929
2. Define your HTTP handlers by implementing the `HttpHandler` interface:
3030
``` java
3131
public class GetDataHandler implements HttpHandler {
32-
// Implement your POST endpoint logic
32+
@Override
33+
public void handle(HttpExchange exchange) throws IOException {
34+
try (exchange) {
35+
byte[] bytes = """
36+
{
37+
"id": "some-id"
38+
}""".getBytes();
3339

34-
// Example
35-
try (exchange) {
36-
byte[] bytes = """
37-
{
38-
"id": "some-id"
39-
}""".getBytes();
40-
41-
try (var os = exchange.getResponseBody()) {
4240
var responseHeaders = exchange.getResponseHeaders();
4341
responseHeaders.add("content-type", "application/json");
4442

4543
exchange.sendResponseHeaders(HTTP_OK, bytes.length);
4644

47-
os.write(bytes);
45+
try (var os = exchange.getResponseBody()) {
46+
os.write(bytes);
47+
}
4848
}
4949
}
5050
}
5151

52-
public class PostDataHandler implements HttpHandler, GetRequestBody {
53-
// Implement your POST endpoint logic
52+
public class PostDataHandler implements HttpHandler {
53+
@Override
54+
public void handle(HttpExchange exchange) throws IOException {
55+
try (exchange) {
56+
// Access the raw request body bytes.
57+
byte[] body = Request.bytes(exchange);
58+
// Or get the already-parsed object (Map or List) produced by your JsonMapper.
59+
Object parsed = Request.parsed(exchange);
60+
61+
exchange.sendResponseHeaders(HTTP_OK, -1);
62+
}
63+
}
5464
}
5565
```
5666

57-
1. Initialize the server (using Gson in this example):
67+
3. Initialize the server (using Gson in this example):
5868
``` java
5969
public class YourServerLauncher {
6070
public static void main(String[] args) throws Exception {
61-
final Gson gson = new Gson();
71+
Gson gson = new Gson();
6272

63-
// Parse OpenAPI specification (or build your instance of OpenApi manually)
64-
var specification = parseSpecification("openapi.json", s -> gson.fromJson(s, OpenApi.class));
73+
// Parse spec to a generic Map (works for JSON; for YAML use SnakeYAML).
74+
String text = Files.readString(Path.of("openapi.json"));
75+
Map<String, Object> raw = (Map<String, Object>) gson.fromJson(text, Map.class);
76+
Spec spec = Spec.from(raw);
6577

66-
// Register your handlers (operation-id -> handler)
78+
// Body parser. Returns a Map for objects, List for arrays.
79+
JsonMapper mapper = body -> gson.fromJson(new String(body), Object.class);
80+
81+
// Handlers by operationId.
6782
Map<String, HttpHandler> handlers = new HashMap<>();
6883
handlers.put("get-data", new GetDataHandler());
6984
handlers.put("post-data", new PostDataHandler());
7085

71-
// Create JSON mapper (supports both arrays and objects)
72-
JsonMapper mapper = new JsonMapper() {
73-
@Override
74-
public <T> T mapFrom(byte[] body) {
75-
if (body.length > 0 && body[0] == '[') {
76-
return (T) gson.fromJson(new String(body), List.class);
77-
}
78-
return (T) gson.fromJson(new String(body), Map.class);
79-
}
80-
};
81-
82-
ExceptionHandler exceptionHandler = Handlers.defaultExceptionHandler();
83-
84-
// Start the server
85-
new OpenApiServer(specification, mapper, handlers, exceptionHandler);
86+
new OpenApiServer(spec, mapper, handlers, Handlers.defaultExceptionHandler());
8687
}
8788
}
8889
```
8990

91+
### YAML specifications
92+
For YAML, replace the JSON parsing line with SnakeYAML:
93+
``` java
94+
Map<String, Object> raw = new Yaml().load(Files.newInputStream(Path.of("openapi.yaml")));
95+
```
96+
The rest is identical.
97+
9098
## Features
9199
- OpenAPI specification support
92100
- Automatic request body parsing for JSON arrays and objects

0 commit comments

Comments
 (0)