Skip to content

Helpers for sending a JSON body with the JDK's built-in HTTP server.

License

Notifications You must be signed in to change notification settings

bowbahdoe/jdk-httpserver-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Json support for the JDK Http Server

javadoc Tests

Utilities for sending JSON responses with the JDK's built-in HTTP server.

Requires Java 21+

Dependency Information

Maven

<dependency>
    <groupId>dev.mccue</groupId>
    <artifactId>jdk-httpserver-json</artifactId>
    <version>2024.05.08</version>
</dependency>

Gradle

dependencies {
    implementation("dev.mccue:jdk-httpserver-json:2024.05.08")
}

Usage

import com.sun.net.httpserver.HttpServer;
import dev.mccue.jdk.httpserver.HttpExchangeUtils;
import dev.mccue.jdk.httpserver.json.JsonBody;
import dev.mccue.json.Json;

import java.io.IOException;
import java.net.InetSocketAddress;

void main() throws IOException {
    var server = HttpServer.create(new InetSocketAddress(8000), 0);
    server.createContext("/", exchange -> {
        HttpExchangeUtils.sendResponse(exchange, 200, JsonBody.of(
                Json.objectBuilder()
                        .put("Hello", "world")
        ));
    });
    server.start();
}

About

Helpers for sending a JSON body with the JDK's built-in HTTP server.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages