use of io.servicetalk.examples.http.serialization.json.PojoResponse in project servicetalk by apple.
the class BlockingPojoStreamingClient method main.
public static void main(String[] args) throws Exception {
try (BlockingStreamingHttpClient client = HttpClients.forSingleAddress("localhost", 8080).buildBlockingStreaming()) {
BlockingStreamingHttpResponse response = client.request(client.post("/pojos").payloadBody(asList(new CreatePojoRequest("value1"), new CreatePojoRequest("value2"), new CreatePojoRequest("value3")), REQ_STREAMING_SERIALIZER));
System.out.println(response.toString((name, value) -> value));
// the full response payload body is drained in case of exceptions
try (BlockingIterator<PojoResponse> payload = response.payloadBody(RESP_STREAMING_SERIALIZER).iterator()) {
while (payload.hasNext()) {
System.out.println(payload.next());
}
}
}
}
use of io.servicetalk.examples.http.serialization.json.PojoResponse in project servicetalk by apple.
the class BlockingPojoStreamingUrlClient method main.
public static void main(String[] args) throws Exception {
try (BlockingStreamingHttpClient client = HttpClients.forMultiAddressUrl().buildBlockingStreaming()) {
BlockingStreamingHttpResponse response = client.request(client.post("http://localhost:8080/pojos").payloadBody(asList(new CreatePojoRequest("value1"), new CreatePojoRequest("value2"), new CreatePojoRequest("value3")), REQ_STREAMING_SERIALIZER));
System.out.println(response.toString((name, value) -> value));
// the full response payload body is drained in case of exceptions
try (BlockingIterator<PojoResponse> payload = response.payloadBody(RESP_STREAMING_SERIALIZER).iterator()) {
while (payload.hasNext()) {
System.out.println(payload.next());
}
}
}
}
use of io.servicetalk.examples.http.serialization.json.PojoResponse in project servicetalk by apple.
the class BlockingPojoStreamingServer method main.
public static void main(String[] args) throws Exception {
HttpServers.forPort(8080).listenBlockingStreamingAndAwait((ctx, request, response) -> {
if (!"/pojos".equals(request.requestTarget())) {
response.status(NOT_FOUND).sendMetaData().close();
} else if (!POST.equals(request.method())) {
response.status(METHOD_NOT_ALLOWED).addHeader(ALLOW, POST.name()).sendMetaData().close();
} else {
BlockingIterable<CreatePojoRequest> values = request.payloadBody(REQ_STREAMING_SERIALIZER);
response.status(CREATED);
try (HttpPayloadWriter<PojoResponse> writer = response.sendMetaData(RESP_STREAMING_SERIALIZER)) {
for (CreatePojoRequest req : values) {
writer.write(new PojoResponse(ID_GENERATOR.getAndIncrement(), req.getValue()));
}
}
}
}).awaitShutdown();
}
Aggregations