javajsonvert.xvertx-httpclient

In Vert.x web client, can I map a JSON response to a collection of POJOs?


In Vert.x Web client manual there's an example of decoding an incoming JSON response into a POJO:

client
  .get(8080, "myserver.mycompany.com", "/some-uri")
  .as(BodyCodec.json(User.class))
  .send(ar -> {
      // Process the response
   })

Is there a way to decode an incoming JSON array into a collection of objects?


Solution

  • I don't believe you can use a BodyCodec to convert the content straight to a collection of objects.

    However you use Vert.x core Json class with the body as Buffer

    client
      .get(8080, "myserver.mycompany.com", "/some-uri")
      .send(ar -> {
        if (ar.succeeded()) {
          Buffer body = ar.result().body();
          List<User> users = Json.decodeValue(body, new TypeReference<List<User>>() {});
        } else {
          // ...
        }
      });