在 Vert.x 网络客户端中,我可以将 JSON 响应映射到 POJO 集合吗?
In Vert.x web client, can I map a JSON response to a collection of POJOs?
在 Vert.x Web client manual 中有一个将传入的 JSON 响应解码为 POJO 的示例:
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.as(BodyCodec.json(User.class))
.send(ar -> {
// Process the response
})
有没有办法将传入的 JSON 数组解码为对象集合?
我认为您不能使用 BodyCodec
将内容直接转换为对象集合。
但是你使用 Vert.x 核心 Json
class 正文为 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 {
// ...
}
});
在 Vert.x Web client manual 中有一个将传入的 JSON 响应解码为 POJO 的示例:
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.as(BodyCodec.json(User.class))
.send(ar -> {
// Process the response
})
有没有办法将传入的 JSON 数组解码为对象集合?
我认为您不能使用 BodyCodec
将内容直接转换为对象集合。
但是你使用 Vert.x 核心 Json
class 正文为 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 {
// ...
}
});