KO
|
EN
gitlite — search
Search
#typescript
#ai-agents
#deepseek-harness
#dsh-plugin
#open-source
#ai
#claude-code
#cli
#windows
#codex
#developer-tools
#dsh
avaje-jex
★ 56
Open GitHub ↗
Web Routing for the JDK Http server
Download README (.md)
Explore Similar Repositories
avaje-validator
:
POJO validation using annotation processing
avaje-prisms
:
Upgraded fork of hickory (updated to Java 11 with module-info and new features)
avaje-http-client
:
A light weight wrapper to the JDK HttpClient
avaje-record-builder
:
generates builder for records
avaje-webview
:
Java implementation of webview
// repository documentation
Was this content helpful?
★ 0
(0 ratings)
Select Rating:
★
★
★
★
★
Submit Feedback
Recent Feedback
×
Download README
Do you want to download the
README.md
file for
avaje-jex
?
Download (.md)
 [](https://discord.gg/Qcqf9R27BR) [](https://github.com/avaje/avaje-jex/actions/workflows/build.yml) [](https://github.com/avaje/avaje-jex/actions/workflows/jdk-ea.yml) [](https://github.com/avaje/avaje-jex/blob/master/LICENSE) [](https://mvnrepository.com/artifact/io.avaje/avaje-jex) [](https://javadoc.io/doc/io.avaje/avaje-jex) # [Avaje-Jex](https://avaje.io/jex/) Lightweight (~120KB) wrapper over the JDK's built-in [HTTP server](https://docs.oracle.com/en/java/javase/23/docs/api/jdk.httpserver/module-summary.html). Features: - [Context](https://javadoc.io/doc/io.avaje/avaje-jex/latest/io.avaje.jex/io/avaje/jex/http/Context.html) abstraction over `HttpExchange` to easily retrieve and send request/response data. - Fluent API - Static resource handling - Server Sent Events - Compression SPI - Json SPI - File Uploads - SSL/mTLS configuration - Virtual threads enabled by default - Multi-Server with any implementation of `jdk.httpserver` (built-in, Grizzly, Jetty, Robaho, Flupke, etc.) ## Quick Start Add dependency: ```xml <dependency> <groupId>io.avaje</groupId> <artifactId>avaje-jex</artifactId> <version>${jex.version}</version> </dependency> ``` Create Server: ```java Jex.create() .get("/", ctx -> ctx.text("hello")) .get("/one/{id}", ctx -> ctx.text("one-" + ctx.pathParam("id"))) .filter( (ctx, chain) -> { System.out.println("before request"); chain.proceed(); System.out.println("after request"); }) .error( IllegalStateException.class, (ctx, exception) -> ctx.status(500).text(exception.getMessage())) .port(8080) .start(); ``` ## Use with [Avaje Http](https://avaje.io/http/) If you find yourself pining for the JAX-RS style of controllers, you can have avaje http generate jex adapters for your annotated classes. ### Add dependencies [](https://mvnrepository.com/artifact/io.avaje/avaje-jex) ```xml <dependency> <groupId>io.avaje</groupId> <artifactId>avaje-jex</artifactId> <version>${jex.version}</version> </dependency> <dependency> <groupId>io.avaje</groupId> <artifactId>avaje-http-api</artifactId> <version>${avaje.http.version}</version> </dependency> <!-- Annotation processor --> <dependency> <groupId>io.avaje</groupId> <artifactId>avaje-http-jex-generator</artifactId> <version>${avaje.http.version}</version> <scope>provided</scope> <optional>true</optional> </dependency> ``` #### JDK 23+ In JDK 23+, annotation processors are disabled by default, you will need to add a flag to re-enable. ```xml <properties> <maven.compiler.proc>full</maven.compiler.proc> </properties> ``` ### Define a Controller ```java package org.example.hello; import io.avaje.http.api.Controller; import io.avaje.http.api.Get; import java.util.List; @Controller("/widgets") public class WidgetController { private final HelloComponent hello; public WidgetController(HelloComponent hello) { this.hello = hello; } @Get("/{id}") Widget getById(int id) { return new Widget(id, "you got it"+ hello.hello()); } @Get() List<Widget> getAll() { return List.of(new Widget(1, "Rob"), new Widget(2, "Fi")); } record Widget(int id, String name){}; } ``` This will generate routing code that we can register using any JSR-330 compliant DI: ```java @Generated("avaje-jex-generator") @Singleton public class WidgetController$Route implements Routing.HttpService { private final WidgetController controller; public WidgetController$Route(WidgetController controller) { this.controller = controller; } @Override public void add(Routing routing) { routing.get("/widgets/{id}", this::_getById); routing.get("/widgets", this::_getAll); } private void _getById(Context ctx) throws IOException { ctx.status(200); var id = asInt(ctx.pathParam("id")); ctx.json(controller.getById(id)); } private void _getAll(Context ctx) throws IOException { ctx.status(200); ctx.json(controller.getAll()); } } ``` ### JSR-330 DI Usage You can use whatever DI library you like. ```java public class Main { public static void main(String[] args ) { List<Routing.HttpService> services = // Retrieve HttpServices via DI; Jex.create().routing(services).start(); } } ``` ## Alternate `HttpServer` Implementations The JDK provides an SPI to swap the underlying `HttpServer`, so you can easily use jex with alternate implementations by adding them as a dependency. ### Robaho [@robaho's httpserver](https://github.com/robaho/httpserver?tab=readme-ov-file#performance) is a zero-dependency implementation that seems to increase performance by 10x over the built-in implementation, and 5x over Jetty in certain benchmarks. [](https://mvnrepository.com/artifact/io.github.robaho/httpserver) ```xml <dependency> <groupId>io.avaje</groupId> <artifactId>avaje-jex</artifactId> <version>${jex.version}</version> </dependency> <dependency> <groupId>io.github.robaho</groupId> <artifactId>httpserver</artifactId> <version>${robaho.version}</version> </dependency> ``` ### Eclipse Jetty [Jetty](https://jetty.org/) is a classic embedded server with a long and distinguished history. [](https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-http-spi) ```xml <dependency> <groupId>io.avaje</groupId> <artifactId>avaje-jex</artifactId> <version>${jex.version}</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-http-spi</artifactId> <version>${jetty.version}</version> </dependency> ``` ### Grizzly [Grizzly](https://github.com/eclipse-ee4j/glassfish-grizzly) is the webserver that powers jakarta EE's opensource Glassfish. [](https://mvnrepository.com/artifact/io.avaje/avaje-jex-grizzly-spi) ```xml <dependency> <groupId>io.avaje</groupId> <artifactId>avaje-jex</artifactId> <version>${jex.version}</version> </dependency> <dependency> <groupId>io.avaje</groupId> <artifactId>avaje-jex-grizzly-spi</artifactId> <version>${plugin.version}</version> </dependency> ``` ### Flupke (HTTP/3, WebTransport) [Flupke](https://github.com/ptrd/flupke) is the first and (at the time of writing) __only__ HTTP/3 implementation written in pure java. [](https://mvnrepository.com/artifact/io.avaje/avaje-jex-http3-flupke) ```xml <dependency> <groupId>io.avaje</groupId> <artifactId>avaje-jex</artifactId> <version>${jex.version}</version> </dependency> <dependency> <groupId>io.avaje</groupId> <artifactId>avaje-jex-http3-flupke</artifactId> <version>${plugin.version}</version> </dependency> ``` ```java var ssl = //HTTP/3 requires https SslPlugin.create(s -> s.keystoreFromClasspath("/keystore.p12", "password")); var jex = Jex.create() .plugin(ssl) .get("/", ctx -> ctx.text("hello (http3)")) .start(); ```