Skip to content

Instantly share code, notes, and snippets.

@cescoffier
Created December 7, 2017 14:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cescoffier/d8fe1334c971504faad1200490929a58 to your computer and use it in GitHub Desktop.
Save cescoffier/d8fe1334c971504faad1200490929a58 to your computer and use it in GitHub Desktop.
Example how to use Collection Codec with Vert.x Web Client
package com.example.demo;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.codec.BodyCodec;
import io.vertx.ext.web.codec.impl.BodyCodecImpl;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Just a few codec to handle collections
*
* @author <a href="http://escoffier.me">Clement Escoffier</a>
*/
public class CollectionCodecs {
public static <T> io.vertx.reactivex.ext.web.codec.BodyCodec<List<T>>
rxList
(Class<T> clazz) {
return new io.vertx.reactivex.ext.web.codec.BodyCodec<>
(list(clazz));
}
public static <T> io.vertx.reactivex.ext.web.codec.BodyCodec<List<T>>
rxSet
(Class<T> clazz) {
return new io.vertx.reactivex.ext.web.codec.BodyCodec<>(set(clazz));
}
public static <T> BodyCodec<List<T>> list(Class<T> clazz) {
return new BodyCodecImpl<>(
buffer -> buffer.toJsonArray()
.stream()
.map(x -> ((JsonObject) x).mapTo(clazz))
.collect(Collectors.toList()));
}
public static <T> BodyCodec<Set<T>> set(Class<T> clazz) {
return new BodyCodecImpl<>(
buffer -> buffer.toJsonArray()
.stream()
.map(x -> ((JsonObject) x).mapTo(clazz))
.collect(Collectors.toSet()));
}
}
package com.example.demo;
import io.vertx.core.json.Json;
import io.vertx.reactivex.core.Vertx;
import io.vertx.reactivex.ext.web.client.HttpResponse;
import io.vertx.reactivex.ext.web.client.WebClient;
import java.util.Arrays;
import java.util.List;
public class Example {
private static List<Person> LIST = Arrays.asList(
new Person("Luke", "Skywalker"),
new Person("C", "3PO"),
new Person("Darth", "Vader")
);
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.createHttpServer()
.requestHandler(req -> {
req.response().end(Json.encode(LIST));
})
.listen(8080);
WebClient client = WebClient.create(vertx);
client.getAbs("http://localhost:8080")
.as(CollectionCodecs.rxList(Person.class))
.rxSend()
.map(HttpResponse::body)
.subscribe(System.out::println);
}
private static class Person {
private String firstName;
private String lastName;
public Person() {
// Empty constructor used by mapper.
}
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public Person setFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public String getLastName() {
return lastName;
}
public Person setLastName(String lastName) {
this.lastName = lastName;
return this;
}
@Override
public String toString() {
return new org.apache.commons.lang3.builder.ToStringBuilder
(this)
.append("firstName", firstName)
.append("lastName", lastName)
.toString();
}
}
}
@cgm-aw
Copy link

cgm-aw commented Jun 29, 2022

Excellent piece of code, also works flawlessly with the non-reactive codec:

public static <T> io.vertx.ext.web.codec.BodyCodec<List<T>> list(Class<T> clazz) {
  return new io.vertx.ext.web.codec.impl.BodyCodecImpl<>(
    buffer -> buffer.toJsonArray()
      .stream()
      .map(x -> ((JsonObject) x).mapTo(clazz))
      .collect(Collectors.toList()));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment