All Projects → avaje → avaje-http

avaje / avaje-http

Licence: Apache-2.0 license
Controller generation for Javalin, Helidon SE.

Programming Languages

java
68154 projects - #9 most used programming language
Dockerfile
14818 projects

Projects that are alternatives of or similar to avaje-http

Joy2OpenVR
Interface to OpenVR Input Emulator to translate any Direct Input controllers commands into VR Controller commands
Stars: ✭ 25 (+8.7%)
Mutual labels:  controllers
Kotlin-Annotation-Processor
Annotation Processor Sample in Kotlin
Stars: ✭ 19 (-17.39%)
Mutual labels:  annotation-processor
heterogeneous-microservices
Implementation of the same simple microservice on different frameworks
Stars: ✭ 43 (+86.96%)
Mutual labels:  helidon
kiwi
Built using only nodejs core. Lightweight, intuitive together with great performance and response timing.
Stars: ✭ 45 (+95.65%)
Mutual labels:  controllers
vigilant
a security controller for Kubernetes
Stars: ✭ 15 (-34.78%)
Mutual labels:  controllers
green-annotations
An Android Annotations plugin to support Green Robot.
Stars: ✭ 21 (-8.7%)
Mutual labels:  annotation-processor
Kotlin Compile Testing
A library for testing Kotlin and Java annotation processors, compiler plugins and code generation
Stars: ✭ 245 (+965.22%)
Mutual labels:  annotation-processor
aptk
A toolkit project to enable you to build annotation processors more easily
Stars: ✭ 28 (+21.74%)
Mutual labels:  annotation-processor
Sage9-Woocommerce-Integration
How to use Woocommerce (3.4.3) with Sage 9.0.1 (Blade + SoberWP controllers), WP 4.9.7 (17/07/2018)
Stars: ✭ 26 (+13.04%)
Mutual labels:  controllers
HadesVR
The "DIY" SteamVR compatible VR setup made for tinkerers.
Stars: ✭ 88 (+282.61%)
Mutual labels:  controllers
Routing Controllers
Create structured, declarative and beautifully organized class-based controllers with heavy decorators usage in Express / Koa using TypeScript and Routing Controllers Framework.
Stars: ✭ 3,557 (+15365.22%)
Mutual labels:  controllers
Responders
A set of Rails responders to dry up your application
Stars: ✭ 1,925 (+8269.57%)
Mutual labels:  controllers
extclassgenerator
Ext JS code generator. Creating model js classes from java classes
Stars: ✭ 14 (-39.13%)
Mutual labels:  annotation-processor
joycon
Device access library for Joycon(Nintendo Switch)
Stars: ✭ 59 (+156.52%)
Mutual labels:  controllers
MethodScope
Reduce repetitive inheritance works in OOP world using @MethodScope.
Stars: ✭ 33 (+43.48%)
Mutual labels:  annotation-processor
gamepad.js
A simple HTML5 Gamepad handler that provides keyboard-like events for Gamepad sticks and buttons.
Stars: ✭ 21 (-8.7%)
Mutual labels:  controllers
COCO-Assistant
Helper for dealing with MS-COCO annotations
Stars: ✭ 83 (+260.87%)
Mutual labels:  annotation-processor
jvmbuilder
A source code generator for Kotlin data classes to automatically create a Builder class.
Stars: ✭ 16 (-30.43%)
Mutual labels:  annotation-processor
navigator
Annotation processor that eliminates navigation and Bundle boilerplate
Stars: ✭ 13 (-43.48%)
Mutual labels:  annotation-processor
sockshop
(START HERE) Helidon Sock Shop: Main application repository containing checkout, build and deployment scripts for all services
Stars: ✭ 93 (+304.35%)
Mutual labels:  helidon

avaje-http

Http server and client libraries and code generation.

Http Server

A jax-rs style controllers with annotations (@Path, @Get ...) that is lightweight by using source code generation (annotation processors) to generate adapter code for Javalin and Helidon SE/Nima.

  • Lightweight as in 65Kb library + generated source code
  • Full use of Javalin or Helidon SE/Nima as desired

Add dependencies

<dependency>
  <groupId>io.avaje</groupId>
  <artifactId>avaje-http-api</artifactId>
  <version>${avaje.http.version}</version>
</dependency>

Add the generator module for your desired microframework as a annotation processor.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>${maven-compiler-plugin.version}</version>
      <configuration>
        <annotationProcessorPaths>
          <path>
            <groupId>io.avaje</groupId>
            <artifactId>avaje-http-javalin-generator</artifactId>
            <version>${avaje.http.version}</version>
          </path>
        </annotationProcessorPaths>
      </configuration>
    </plugin>
  </plugins>
</build>

Define a Controller (These APT processors work with both Java and Kotlin.)

package org.example.hello;

import io.avaje.http.api.Controller;
import io.avaje.http.api.Get;
import io.avaje.http.api.Path;
import java.util.List;

@Path("/widgets")
@Controller
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){};
}

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:

var routes = BeanScope.builder().build().list(WebRoutes.class); 

Javalin.create()
        .routes(() -> routes.forEach(WebRoutes::registerRoutes))
        .start();

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.

var routes = BeanScope.builder().build().list(Service.class); 
var routingBuilder = Routing.builder().register(routes.stream().toArray(Service[]::new));
WebServer.builder()
        .addMediaSupport(JacksonSupport.create())
        .routing(routingBuilder)
        .build()
        .start();

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.

var routes = BeanScope.builder().build().list(HttpService.class); 
final var builder = HttpRouting.builder();

for (final HttpService httpService : routes) {
   httpService.routing(builder);
}

WebServer.builder()
         .addRouting(builder.build())
         .build()
         .start();

Generated sources

(Javalin) The generated WidgetController$Route.java is:

@Generated("avaje-javalin-generator")
@Component
public class WidgetController$Route implements WebRoutes {

  private final WidgetController controller;

  public WidgetController$Route(WidgetController controller) {
    this.controller = controller;
  }

  @Override
  public void registerRoutes() {

    ApiBuilder.get("/widgets/{id}", ctx -> {
      ctx.status(200);
      var id = asInt(ctx.pathParam("id"));
      var result = controller.getById(id);
      ctx.json(result);
    });

    ApiBuilder.get("/widgets", ctx -> {
      ctx.status(200);
      var result = controller.getAll();
      ctx.json(result);
    });

  }

}

(Helidon SE) The generated WidgetController$Route.java is:

@Generated("io.dinject.helidon-generator")
@Singleton
public class WidgetController$Route implements Service {

  private final WidgetController controller;

  public WidgetController$Route(WidgetController controller) {
    this.controller = controller;
  }

  @Override
  public void update(Routing.Rules rules) {

    rules.get("/widgets/{id}", this::_getById);
    rules.post("/widgets", this::_getAll);
  }

  private void _getById(ServerRequest req, ServerResponse res) {
    int id = asInt(req.path().param("id"));
    res.send(controller.getById(id));
  }

  private void _getAll(ServerRequest req, ServerResponse res) {
    res.send(controller.getAll());
  }

}

(Helidon Nima) The generated WidgetController$Route.java is:

@Generated("avaje-helidon-nima-generator")
@Component
public class WidgetController$Route implements HttpService {

  private final WidgetController controller;
  public WidgetController$Route(WidgetController controller) {
    this.controller = controller;
  }

  @Override
  public void routing(HttpRules rules) {
    rules.get("/widgets/{id}", this::_getById);
    rules.get("/widgets", this::_getAll);
  }

  private void _getById(ServerRequest req, ServerResponse res) {
    var pathParams = req.path().pathParameters();
    int id = asInt(pathParams.first("id").get());
    var result = controller.getById(id);
    res.send(result);
  }

  private void _getAll(ServerRequest req, ServerResponse res) {
    var pathParams = req.path().pathParameters();
    var result = controller.getAll();
    res.send(result);
  }

}

Generated sources (Avaje-Jsonb)

If Avaje-Jsonb is detected, http generators with support will use it for faster Json message processing.

(Javalin) The generated WidgetController$Route.java is:

@Generated("avaje-javalin-generator")
@Component
public class WidgetController$Route implements WebRoutes {

  private final WidgetController controller;
  private final JsonType<java.util.List<org.example.hello.WidgetController.Widget>> listWidgetJsonType;
  private final JsonType<org.example.hello.WidgetController.Widget> 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);
  }

  @Override
  public void registerRoutes() {

    ApiBuilder.get("/widgets/{id}", ctx -> {
      ctx.status(200);
      var id = asInt(ctx.pathParam("id"));
      var result = controller.getById(id);
      widgetJsonType.toJson(result, ctx.contentType("application/json").outputStream());
    });

    ApiBuilder.get("/widgets", ctx -> {
      ctx.status(200);
      var result = controller.getAll();
      listWidgetJsonType.toJson(result, ctx.contentType("application/json").outputStream());
    });

  }

}

(Helidon Nima) The generated WidgetController$Route.java is:

@Generated("avaje-helidon-nima-generator")
@Component
public class WidgetController$Route implements HttpService {


  private final WidgetController controller;
  private final JsonType<org.example.hello.WidgetController.Widget> widgetJsonType;
  private final JsonType<java.util.List<org.example.hello.WidgetController.Widget>> 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();
  }

  @Override
  public void routing(HttpRules rules) {
    rules.get("/widgets/{id}", this::_getById);
    rules.get("/widgets", this::_getAll);
  }

  private void _getById(ServerRequest req, ServerResponse res) {
    var pathParams = req.path().pathParameters();
    int id = asInt(pathParams.first("id").get());
    var result = controller.getById(id);
    res.headers().contentType(io.helidon.common.http.HttpMediaType.APPLICATION_JSON);
    widgetJsonType.toJson(result, res.outputStream());
  }

  private void _getAll(ServerRequest req, ServerResponse res) {
    var pathParams = req.path().pathParameters();
    var result = controller.getAll();
    res.headers().contentType(io.helidon.common.http.HttpMediaType.APPLICATION_JSON);
    listWidgetJsonType.toJson(result, res.outputStream());
  }

}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].