• Stars
    star
    120
  • Rank 295,983 (Top 6 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Annotation layer on top of Vert.x 3

⚠️ tl;dr: don't use this repository, use something like Quarkus instead. ⚠️

Unfortunately, this project relies on an old version of Vert.x. Upgrading it to Vert.x 3.5 requires a lot of work, I am looking at it, but since it's a "weekend" project, it might take months. Sorry for the inconvenience. Obviously any kind of help would be greatly appreciated. Thank you for your comprehension.

If you feel like you want to help, any pull request would be highly welcomed. You can also fork the project, and start working on your own stuff, with your own vision of what such an annotation framework would be to suit your needs. The license allows you to do so.

You can start by forking the project, changing Vert.x dependency to 3.5 get it to compile (which requires some work) then run the tests (there's a pretty good code coverage) and start fixing stuff from there. That's probably the best way to help.

As an alternative, you can have a look at Quarkus, which adds JAX-RS annotation capabilities on top of Vert.x (with a lot of other interesting modules).

Special thanks to johnfg10 for upgrading to 3.5.2.


Build Status Codecov

Vert.x Nubes

Provides an annotation layer on top of vertx-web.

Declare your Vert.x routes with annotated methods and controllers, in a Spring MVC-ish way.

repositories {
  jcenter()
}

dependencies {
  compile 'com.github.aesteve:nubes:2.0'
}

Declarative

Nubes automatically injects method parameters at runtime so that you can express your routes in a declarative manner. By reading the signature of the method, you should be able to have a quick glimpse at what your route uses (query parameters, request body, ...) and produces (void => status 204, object => marshalled).

public PeanutsCharacter get(@Param CharacterType type) lets us know that the method uses a request parameter named type and that the response will contain a marshalled PeanutsCharacter POJO.

Nubes comes with a controller layer, but also a service layer. You can declare services as simple POJOS and they'll be injected within your controllers, you can also declare your services as async, they'll be started when your application starts, stopped when the application stops.

Extensible

The framework is designed to be fully extensible so that you can register and use your own annotations, whether it's an interceptor, a type-adapter, ...

A good example on how to extend the framework is Nubes Mongo, a set of additionnal utilities (annotations, interceptors, ...) designed to help you deal with Mongo on top of Nubes. For example, nubes-mongo registers a @Create annotation against Nubes framework, meaning that the result of the method should be saved against the Mongo database.

Basically, an annotation will be tied to a set of Vert.x's Handlers, executed before and/or after the 'route' method is executed.

Non-blocking (obviously) but also non-stumbling

Even though Nubes looks opinionated (declaring your routes in a single way : the controller/method way), keep in mind that at the end of the day, vertx-web's router is still there and fully accessible if you find yourself stuck in an edge case Nubes isn't designed to handle. This way, you should never, ever be stucked.

You just have a set of additional utilities at your disposal to declare your web routes in another way (a SpringMVC-ish way), to declare and inject services if you don't have a DI framework at your disposal, or to write Verticles differently.

The Router can also be injected as a field within your controllers as an easy way to deal with it, and maybe register routes at runtime if you need to.

Examples

(Work in Progress)

If you're interested in how a real-life project built with Nubes would look like, you can have a look at Nubes Chat a very simple chat relying on a REST API and Vert.x's event-bus bridge which shows both REST and Socket controllers in action. On top of that, it uses a MongoDB as a persistent store so you can get a nice view of the service layer.

Basic example :

A controller :

package com.peanuts.controllers;

@Controller("/peanuts")
public class PeanutsPages {
  
  public ENUM CharacterType {
    DOG, GIRL, BOY, BIRD;
  }
  
  @GET("/character")
  @View
  public String getCharacter(@ContextData Map<String, Object> data, @Param CharacterType type) {
    switch(type) {
      case DOG: 
        data.put("name", "Snoopy");
        break;
      case BOY:
        data.put("name", "Charlie Brown");
        break;
      // ...
    }
    return "character.hbs";
  }
}

The view :

web/views/character.hbs

<html>
  <body>
    Hello, I'm a Peanuts character, and my name is {{name}}
  </body>
</html>

GET "/peanuts/character?type=DOG" will return the following html view:

Hello, I'm a Peanuts character, and my name is Snoopy

A JSON api example

package com.peanuts.controllers;

@Controller("/api/1/peanuts")
@ContentType("application/json")
public class CharactersController {
  
  @Service("mongo")
  private MongoService mongo;
  
  @GET("/character")
  public PeanutsCharacter getCharacter(@Param CharacterType type) {
    switch(type) {
      case DOG: 
        return new PeanutsCharacter(CharacterType.DOG, "Snoopy", snoopysBirthDate);
      // etc. 
    }
  }
  
  @POST("/character") // declaring RoutingContext as parameter means your method is async
  public void createCharacter(RoutingContext context, @RequestBody PeanutsCharacter character) {
    mongo.save(character, handler -> {
      context.next();  
    }); // save it using JDBC service, mongo service, hibernate service, etc.
  }
}

With this an example of domain object :

package com.peanuts.model;

public class PeanutsCharacter {

  public ENUM CharacterType {
    DOG, GIRL, BOY, BIRD;
  }

  private CharacterType type;
  private String name;
  private Date birthDate;
  
  // getters, setters, constructors and stuff
}

GET "/api/1/peanuts/characters?type=DOG" with an Accept header containing application/json will return :

{
  "name":"Snoopy",
  "type":"DOG",
  "birthDate":"1950-11-04T08:00:00.000Z"
}

POST "/api/1/peanuts/characters" with the following request body :

{
  "name":"Snoopy",
  "type":"DOG",
  "birthDate":"1950-11-04T08:00:00.000Z"
}

Will save our favorite cartoon dog into the database, then return an HTTP 204.

How it works

VertxNubes as the entry point

The entry point of every work with the framwork is creatning a VertxNubes instance.

You'll notice the constructor takes two arguments :

  • a Vertx instance
  • a JsonObject containing the configuration

Please take a look at the configuration documentation for the available, mandatory or not, options.

Once you've created the VertxNubes instance, you need to bootstrap it. What it's gonna do is scanning your application classes (annotated with @Controller) in order to create the approriate Web routes/handlers and attach it to a vertx-web Router.

You can provide your own Router, if you want to to add custom routes and stuff in the standard vertx way.

You can also let VertxNubes instanciate a Router. It's gonna return it to you once it's done bootstrapping. And you'll be able to do pretty much whatever you need with it.

VertxNubes nubes = new VertxNubes(vertx, config);
nubes.bootstrap(res -> {
  if (res.succeeded()) {
    Router yourRouter = res.result();
    System.out.println("Everything's ready");
  } else {
    System.err.println("Something went wrong");
    res.cause().printStackTrace();
  }
});

You'll find a ton of examples in the tests of the project.

If you take a look at the mock controllers, you'll pretty much find everything that's possible to do with Nubes out of the box.

The Controller layer

What is a @Controller ?

A controller is a Java singleton (per Nubes instance) defining a set of methods which will be translated into vertx-web handlers. (~= express middlewares).

It's important that your controller defines a no-argument constructor, VertxNubes expect that.

In a controller you'll find routes, annotated with @GET, @POST, @PUT, ... but also filters of two differents types : @BeforeFilter and @AfterFilter.

For each route in your controller, before filters will be executed before your actual route method, and after filters, well... after.

Annotations

Nubes provides some default annotations. Here's the list.

But you can also define your own annotations, and attach vertx-web handlers to it.

In this case, you can register what Nubes calls "Annotation processors" which will be called before, after (or both) your method is invoked.

Nubes itself registers its own annotations using this API. For example, the @ContentType({"application/json", "application/xml"}) annotation is bound to a ContentTypeProcessor which will :

  • check the Accept header of the request, and if it doesn't matches the MIME type you're handling, return a 406 status to the client
  • find the most suitable MIME among the ones the client specified in its Accept header and the ones you specified in the body of the ContentType annotation
  • inject this ContentType as a variable in the RoutingContext so that you can also benefit from it
  • position the Content-Type response header so that you don't have to care about it

Read the annotations document

Parameters

Parameters are automatically injected into every method at runtime, depending on the context of the request (parameters, body, ...).

For a complete list of available parameters (by default), see the parameters documentation.

But you can also register your own parameter resolvers by telling nubes : "When you find this type of parameter, resolve it like this".

Parameters can be resolved simply by their types (that's how Nubes injects the RoutingContext or the EventBus parameters if your method asks for it) or by a custom annotation you define.

Read the parameters injection documentation

The View Layer

TODO : explain that template engines are created by the user, and bound to a file extension. Then how views are resolved, either by @View("viewName.extension") or through the ViewResolver parameter.

The Service Layer

Services in Nubes are simple POJOs you register on the Nubes instance using nubes.registerService(String name, Object serviceInstance). This way, you'll be able to access them from your controllers using the @Service("someName") annotation.

Standard services

Any POJO can be a service. Simply register it against Nubes.

Async services

In some case, services take time to startup or to be closed. If you want your service to be started when Nubes bootstraps, just implements Nubes Service interface and register it, the same way you would register a POJO.

RPC and service proxies

TODO : Explain how to use vertx's service proxy.

SockJS

Nubes provides a simple way to declare SockJS handlers with annotations instead of handlers defined in vertx-web.

For example, to handle incoming sockets you would do the following.

@SockJS("/sockjs/*")
public class SockJSController {
  @OnMessage
  public void onMessage(SockJSSocket emitter, Buffer message) {
    emitter.write(Buffer.buffer(message)); // simply echo the message back
  }
}

You can also use vertx-web's event-bus bridge if you want your users to access some addresses over the event-bus from client-side using SockJS.

@EventBusBridge("/sockjs/*")
@InboundPermitted("some-allowed-address")
public class BridgeController {

  @SOCKET_CREATED
  public void whenSocketIsCreated(BridgeEvent event) {
    // do what you want...
  }
  
  @REGISTER
  public void whenSomeUserRegisters(BridgeEvent event) {
    // do what you want...
  }
  
}

More Repositories

1

vertx-feeds

Feed aggregator using Vert.x 3 (showcase)
Java
130
star
2

vertx-sse

Add support for Server-Sent-Events in Vert.x Web
Java
47
star
3

vertx-hibernate-service

Hibernate service for Vert.x
Java
19
star
4

grooveex

Extension module for vertx-groovy adding methods and syntaxic sugar
Groovy
14
star
5

vertx-eventbus-client-rs

Rust client for Vert.x TCP event-bus bridge
Rust
10
star
6

vertx-load-balancer

Example on how to write a very simple load balancer using Vert.x
Java
9
star
7

quarkus-scala-gradle

Simple project showing Quarkus working with Scala and Gradle
Kotlin
9
star
8

nashorn-es6

Test to make nashorn compatible with Ecma 6
JavaScript
7
star
9

vertx-kotlin-todomvc

Implementation of the TodoMVC server using Vert.x and Kotlin
Kotlin
7
star
10

http2-showcase

Showcase for http2 Vert.x server vs. standard http
Java
7
star
11

w3rs

Warcraft 3 (Reforged) replay parser, written in Rust
Rust
6
star
12

vertx-markdown-service

Basic Vert.x service to generate html from markdown over the event bus
Java
6
star
13

todo-backend-vertx

Java
5
star
14

fertx

Functional Vert.x Web + Scala (inspired by Akka http)
Scala
4
star
15

vertx-service-registry

Website to show Vert.x 3 available services
Java
4
star
16

vertx-pojo-config

Simple Vert.x extension to map your JsonObject configuration to a standard Java bean
Java
3
star
17

nubes-chat

Example of a chat application built with Nubes framework
JavaScript
3
star
18

projuice

The juicy project manager
Java
2
star
19

connect-fs-source-demo

Very simple demo playing with the filsystem source connector
Dockerfile
2
star
20

quarkus-kotlin-todobackend

Trying to implement todobackend in Kotlin with Quarkus
Kotlin
2
star
21

buska

Search & Find records in Apache Kafka
Rust
2
star
22

vertx-calliope

Add more expressiveness to vertx-web
Java
2
star
23

vertx-web-groovy-dsl

A simple Groovy DSL to declare Vert.x Apex routes
Groovy
2
star
24

vertx-react-isomorphic

Attempt to make an isomorphic React application with vertx-lang-js
JavaScript
2
star
25

todobackend-springboot2

TodoBackend implementation using SpringBoot 2 + reactive mongo + kotlin
Kotlin
2
star
26

todobackend-phoenix

TodoBackend implementation using Phoenix + PostgreSQL
Elixir
1
star
27

kafka-streams-rename-topics-scala

Show how to use a single Kafka Streams app to rename a set of input topics
Scala
1
star
28

reactive-pg-client-pipelining

Trying to reproduce an issue with pipelining
Kotlin
1
star
29

nubes-orm

ORM utilities for Nubes
Java
1
star
30

vertx-scala-todomvc

TodoMVC implementation using Vert.x-lang-scala
Scala
1
star
31

vertx-ceylon-todobackend

TodoBackend implementation in Ceylon using Vert.x
Ceylon
1
star
32

vertx-portal

Everything you need to know about Vert.x
Groovy
1
star
33

nubes-mongo

Mongo implementation of nubes-orm
Java
1
star
34

vertx-mvc-specifications

Specification wiki for a MVC-layer on top of Vert.x 3 stack
1
star
35

rapidoid-todobackend-hibernate

TodoBackend implementation using Rapidoid & JPA (Hibernate)
Java
1
star
36

todo-backend-grooveex

TodoMVC backend using grooveex on top of Vert.x 3
Groovy
1
star
37

quarkus-smallrye-jwt-reproducer

Reproducer showing buildNative fail with smallrye jwt
Kotlin
1
star
38

swapi-finch

Experiment : a gateway using finagle / finch (based on swapi.co)
Scala
1
star
39

vertx-groovy-template-engine

Vertx template engine adapter for Groovy TemplateEngine
Java
1
star
40

itinerarium

PROTOTYPE of a Rust API gateway system - for learning purposes only -
Rust
1
star
41

ceylon-react

simple test
Ceylon
1
star
42

nubes-hibernate

Hibernate utilities (service, interceptors, ...) for Nubes
Java
1
star
43

vertx-dgraph-client

Small example showing how to use vertx-grpc client in order to target a running dgraph service
Java
1
star
44

vertx-git-service

Asynchronous, polyglot service to deal with Git repositories
Java
1
star