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

一个人想着一个人 提交于 2019-12-11 07:05:36

问题


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?


回答1:


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 {
      // ...
    }
  });


来源:https://stackoverflow.com/questions/53804316/in-vert-x-web-client-can-i-map-a-json-response-to-a-collection-of-pojos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!