Rest with Scala and Vert.x

1

A previous post introduced some features of using Scala with Vert.x. This post covers how to publish Rest web services in an elegant and simple fashion.

As in the previous post, Examples in Java and Scala are presented, source code been hosted on GitHub as part of lang-scala https://github.com/ouertani/vert.x/tree/master/vertx-lang/vertx-lang-scala

I- Resticle

Resticle is a unit of deployment dedicated to Rest. It provides a new method (“handles”) which can be used by Vert.x to start Rest services. “handles” returns a sequence of rest handlers :

  • RestHandler : Action => Response
  • Action :
    • GET(pattern : String)
    • POST (pattern : String)
    • PUT (pattern : String)
    • DELETE(pattern : String)
    • HEAD (pattern : String)
    • ALL(pattern : String)
  • Response : HttpServerRequest => Any
    • OK, Unauthorized,….

In this tutorial we will be working with the SampleResticle class, for both scala and java.

II – Hello World

Vert.x provides a powerful routing and route matching mechanism, which simplifies the routing of HTTP requests to different handlers based on pattern matching on the request path.

In Hello World snippet, let us publish a static GET service :

  • GET : /hello → code : 200 , body : world
Scala

Java

III- Chaining Handlers

Using Resticle we can chain handlers quit easily. The following snippets create static GET and DELETE services :

  • GET : /hello → code : 200 , body : world
  • DELETE : /posts → code : 401 , body : Not allowed user
Scala

java

IV – Value path

Vert.x pattern matching lets you extract values from the path and use them as parameters in the request.

  • GET : /hello → code : 200 , body : world
  • DELETE : /posts → code : 401 , body : Not allowed user
  • POST : /:blogname → code : 200 , body : post {blogname} received !
Scala : ( Using String interpolation)

Java

V- Json

Let’s assume we have a Blog class with two String fields title and content :

The java equivalent has been relocated to the end of the document due to its verbosity ;).

Publishing an object using Resticle is simple and transparent due to implicit convertor : T => Buffer.

Scala ( Type Class)

Java ( with explicit convertor )

  • GET : /hello → code : 200 , body : world
  • DELETE : /posts → code : 401 , body : Not allowed user
  • POST : /:blogname → code : 200 , body : post {blogname} received !
  • GET : /:id → code : 200 , body : {« title »: »rest », »content »: »scala & vertx »}
Scala

Java

Java Blog

Conclusion

Despite the fact that Resticle is in first development step, Rest support is by far simpler and elegant in scala than in java. As described in first tutorial Vert.x java version is burdened with a frightening number of handlers. Will Vert.x 2.0 address this point using Promises/Deferred APIs ?

Partagez cet article.

A propos de l'auteur

Un commentaire

  1. Thxs a lot for this tutorial , that’s really interesting !
    I have respected all instructions but finally just the « get » work well and show  » world »
    can u help me , thank you

Ajouter un commentaire