|
| 1 | +# openapi-httpserver-java |
| 2 | + |
| 3 | + |
| 4 | +# OpenAPI Server Library |
| 5 | +A lightweight Java library for creating HTTP servers based on OpenAPI specifications. |
| 6 | + |
| 7 | + |
| 8 | +## Overview |
| 9 | +This library provides a simple way to create an HTTP server that implements OpenAPI specifications. |
| 10 | + |
| 11 | + |
| 12 | +## Getting Started |
| 13 | + |
| 14 | +### Prerequisites |
| 15 | +- Java SDK 21 or later |
| 16 | +- A serialization library, e.g. Gson or Jackson |
| 17 | +- OpenAPI specification file in JSON format (`openapi.json`) |
| 18 | + |
| 19 | + |
| 20 | +### Basic Usage |
| 21 | +1. Create an OpenAPI specification file named `openapi.json` in your project resources. |
| 22 | +2. Define your HTTP handlers by implementing the `HttpHandler` interface: |
| 23 | +``` java |
| 24 | +public class GetDataHandler implements HttpHandler { |
| 25 | + // Implement your POST endpoint logic |
| 26 | + |
| 27 | + // Example |
| 28 | + try (exchange) { |
| 29 | + byte[] bytes = """ |
| 30 | + { |
| 31 | + "id": "some-id" |
| 32 | + }""".getBytes(); |
| 33 | + |
| 34 | + try (var os = exchange.getResponseBody()) { |
| 35 | + var responseHeaders = exchange.getResponseHeaders(); |
| 36 | + responseHeaders.add("content-type", "application/json"); |
| 37 | + |
| 38 | + exchange.sendResponseHeaders(HTTP_OK, bytes.length); |
| 39 | + |
| 40 | + os.write(bytes); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +public class PostDataHandler implements HttpHandler, GetRequestBody { |
| 46 | + // Implement your POST endpoint logic |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +1. Initialize the server (using Gson in this example): |
| 51 | +``` java |
| 52 | +public class YourServerLauncher { |
| 53 | + public static void main(String[] args) throws Exception { |
| 54 | + final Gson gson = new Gson(); |
| 55 | + |
| 56 | + // Parse OpenAPI specification (or build your instance of OpenApi manually) |
| 57 | + var specification = parseSpecification("openapi.json", s -> gson.fromJson(s, OpenApi.class)); |
| 58 | + |
| 59 | + // Register your handlers (operation-id -> handler) |
| 60 | + Map<String, HttpHandler> handlers = new HashMap<>(); |
| 61 | + handlers.put("get-data", new GetDataHandler()); |
| 62 | + handlers.put("post-data", new PostDataHandler()); |
| 63 | + |
| 64 | + // Create JSON mapper (supports both arrays and objects) |
| 65 | + JsonMapper mapper = new JsonMapper() { |
| 66 | + @Override |
| 67 | + public <T> T mapFrom(byte[] body) { |
| 68 | + if (body.length > 0 && body[0] == '[') { |
| 69 | + return (T) gson.fromJson(new String(body), List.class); |
| 70 | + } |
| 71 | + return (T) gson.fromJson(new String(body), Map.class); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + ExceptionHandler exceptionHandler = Handlers.defaultExceptionHandler(); |
| 76 | + |
| 77 | + // Start the server |
| 78 | + new OpenApiServer(specification, mapper, handlers, exceptionHandler); |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## Features |
| 84 | +- OpenAPI specification support |
| 85 | +- Automatic request body parsing for JSON arrays and objects |
| 86 | +- Custom HTTP handler support |
| 87 | +- Built on Java's native `HttpServer` with Thread-Per-Request behaviour using Virtual Threads. |
| 88 | +- Custom integration for JSON serialization/deserialization |
| 89 | + |
| 90 | + |
| 91 | +## Handler Registration |
| 92 | +Handlers are registered using string keys that correspond to your OpenAPI operation IDs. |
| 93 | + |
| 94 | + |
| 95 | +## JSON Mapping |
| 96 | +The library uses a flexible JSON mapping system that automatically detects and parses (using a mapper of choice): |
| 97 | +- JSON arrays (`[...]`) |
| 98 | +- JSON objects (`{...}`) |
| 99 | + |
| 100 | +## Known limitations (not exhaustive..) |
| 101 | + |
| 102 | +- OpenAPI refs are not supported yet. |
| 103 | + |
| 104 | +## Notes |
| 105 | +- The server uses a default configuration when initialized with a null parameter for the last argument |
| 106 | +- Make sure your OpenAPI specification file (`openapi.json`) is accessible in your classpath |
| 107 | +- Custom error handling and logging is provided through SLF4J |
| 108 | + |
| 109 | +This library is designed to be simple to use while providing the essential features needed for creating OpenAPI-compliant HTTP servers in Java. |
0 commit comments