-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
WebClientExample.java
44 lines (38 loc) · 1.33 KB
/
WebClientExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package io.vertx.example.virtualthreads;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.ThreadingModel;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.*;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.predicate.ResponsePredicate;
import static io.vertx.core.Future.await;
public class WebClientExample extends AbstractVerticle {
public static void main(String[] args) throws Exception {
var vertx = Vertx.vertx();
vertx.deployVerticle(WebClientExample.class, new DeploymentOptions()
.setThreadingModel(ThreadingModel.VIRTUAL_THREAD))
.toCompletionStage()
.toCompletableFuture()
.get();
}
@Override
public void start() {
var server = vertx.createHttpServer();
server.requestHandler(request -> {
request.response().end("Hello World");
});
await(server.listen(8080, "localhost"));
// Make a simple HTTP request
var client = WebClient.create(vertx);
var resp = await(client
.get(8080, "localhost", "/")
.expect(ResponsePredicate.SC_OK)
.send());
var status = resp.statusCode();
var body = resp.body();
System.out.println("Got response status=" + status + " body='" + body + "'");
}
}