问题
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