JSON REST rozhranie

Vibe.d allows to quickly implement a JSON webservice. If we want to implement the following JSON output for a HTTP request to /api/v1/chapters:

[
  {
    "title": "Hello",
    "id": 1,
    "sections": [
      {
        "title": "World",
        "id": 1
      }
    ]
  },
  {
    "title": "Advanced",
    "id": 2,
    "sections": []
  }
]

First define an interface that implements the according functions and D structs that are serialized 1:1:

interface IRest
{
    struct Section {
        string title;
        int id;
    }
    struct Chapter {
        string title;
        int id;
        Section[] sections;
    }
    @path("/api/v1/chapters")
    Chapter[] getChapters();
}

To actual fill the data structures, we have to inherit from the interface and implement the business logic:

class Rest: IRest {
    Chapter[] getChapters() {
      // fill
    }
}

Given an URLRouter instance we register an instance of the Rest class and we're done!

auto router = new URLRouter;
router.registerRestInterface(new Rest);

Vibe.d REST interface generator also supports POST request where the children of the posted JSON object are mapped to the member function's parameters.

The REST interface can be used to generate a REST client instance which transparently sends JSON requests to the given server, using the same member functions used on the backend side. This is useful when code is shared between client and server.

auto api = new RestInterfaceClient!IRest("http://127.0.0.1:8080/");
// sends GET /api/v1/chapters and deserializes
// response to IRest.Chapter[]
auto chapters = api.getChapters();

rdmd playground.d