diff --git a/README.md b/README.md index 38df8651e..60334e3a2 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ -# avaje-http +# [Avaje-HTTP](https://avaje.io/http/) +[![Build](https://github.com/avaje/avaje-http/actions/workflows/build.yml/badge.svg)](https://github.com/avaje/avaje-http/actions/workflows/build.yml) + +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/avaje/avaje-inject/blob/master/LICENSE) HTTP server and client libraries via code generation. -Documentation at [avaje.io/http](https://avaje.io/http/) -## Http Server +## HTTP Server A jax-rs style controllers with annotations (`@Path`, `@Get` ...) that is lightweight by using source code generation (annotation processors) @@ -13,37 +15,59 @@ to generate adapter code for Javalin and Helidon SE/Nima. - Full use of Javalin or Helidon SE/Nima as desired ## Add dependencies - ```xml + + io.avaje + avaje-inject + ${avaje-inject.version} + io.avaje avaje-http-api ${avaje.http.version} ``` -Add the generator module for your desired microframework as a annotation processor. +#### Add the generator module for your desired microframework as a annotation processor. ```xml - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - - - io.avaje - avaje-http-javalin-generator - ${avaje.http.version} - - - - - - + + + io.avaje + avaje-inject-generator + ${avaje-inject.version} + provided + + + io.avaje + avaje-http-javalin-generator + ${avaje-http.version} + provided + +``` +If there are other annotation processors and they are specified via maven-compiler-plugin then we add avaje-http-generator there instead. +```xml + + org.apache.maven.plugins + maven-compiler-plugin + + + + io.avaje + avaje-inject-generator + ${avaje-inject.version} + + + io.avaje + avaje-http-javalin-generator + ${avaje-http.version} + + + ... other annotation processor ... + + + + ``` - ## Define a Controller (These APT processors work with both Java and Kotlin.) ```java package org.example.hello; @@ -60,7 +84,7 @@ public class WidgetController { public WidgetController(HelloComponent hello) { this.hello = hello; } - + @Get("/{id}") Widget getById(int id) { return new Widget(id, "you got it"+ hello.hello()); @@ -75,26 +99,31 @@ public class WidgetController { } ``` -## Usage with Javalin +## Usage +The annotation processor will generate controller adapters that can register routes to Javalin/Helidon. The natural way to use the generated adapters is to get a DI library to find and wire them. This is what the below examples do and they use [Avaje-Inject](https://avaje.io/inject/) to do this. + +Note that there isn't a requirement to use Avaje for dependency injection. Any DI library that can find and wire the generated @Singleton beans can be used. You can even use Dagger2 or Guice to wire the controllers if you so desire. + +### Usage with Javalin The annotation processor will generate controller classes implementing the WebRoutes interface, which means we can get all the WebRoutes and register them with Javalin using: ```java -var routes = BeanScope.builder().build().list(WebRoutes.class); +var routes = BeanScope.builder().build().list(WebRoutes.class); Javalin.create() .routes(() -> routes.forEach(WebRoutes::registerRoutes)) .start(); ``` -## Usage with Helidon SE +### Usage with Helidon SE The annotation processor will generate controller classes implementing the Helidon Service interface, which we can use get all the Services and register them with Helidon `RoutingBuilder`. ```java -var routes = BeanScope.builder().build().list(Service.class); +var routes = BeanScope.builder().build().list(Service.class); var routingBuilder = Routing.builder().register(routes.stream().toArray(Service[]::new)); WebServer.builder() .addMediaSupport(JacksonSupport.create()) @@ -103,13 +132,13 @@ WebServer.builder() .start(); ``` -## Usage with Helidon Nima +### Usage with Helidon Nima The annotation processor will generate controller classes implementing the Helidon HttpService interface, which we can use get all the services and register them with the Helidon `HttpRouting`. ```java -var routes = BeanScope.builder().build().list(HttpService.class); +var routes = BeanScope.builder().build().list(HttpService.class); final var builder = HttpRouting.builder(); for (final HttpService httpService : routes) { @@ -232,13 +261,13 @@ If [Avaje-Jsonb](https://github.com/avaje/avaje-jsonb) is detected, http generat public class WidgetController$Route implements WebRoutes { private final WidgetController controller; - private final JsonType> listWidgetJsonType; - private final JsonType widgetJsonType; + private final JsonType> listWidgetJsonType; + private final JsonType widgetJsonType; public WidgetController$Route(WidgetController controller, Jsonb jsonB) { this.controller = controller; - this.listWidgetJsonType = jsonB.type(org.example.hello.WidgetController.Widget.class).list(); - this.widgetJsonType = jsonB.type(org.example.hello.WidgetController.Widget.class); + this.listWidgetJsonType = jsonB.type(Widget.class).list(); + this.widgetJsonType = jsonB.type(Widget.class); } @Override @@ -271,13 +300,13 @@ public class WidgetController$Route implements HttpService { private final WidgetController controller; - private final JsonType widgetJsonType; - private final JsonType> listWidgetJsonType; + private final JsonType widgetJsonType; + private final JsonType> listWidgetJsonType; public WidgetController$Route(WidgetController controller, Jsonb jsonB) { this.controller = controller; - this.widgetJsonType = jsonB.type(org.example.hello.WidgetController.Widget.class); - this.listWidgetJsonType = jsonB.type(org.example.hello.WidgetController.Widget.class).list(); + this.widgetJsonType = jsonB.type(Widget.class); + this.listWidgetJsonType = jsonB.type(Widget.class).list(); } @Override