本文整理了Java中io.vertx.ext.web.client.HttpRequest.as()
方法的一些代码示例,展示了HttpRequest.as()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.as()
方法的具体详情如下:
包路径:io.vertx.ext.web.client.HttpRequest
类名称:HttpRequest
方法名:as
暂无
代码示例来源:origin: vert-x3/vertx-examples
@Override
public void start() throws Exception {
WebClient client = WebClient.create(vertx);
client.get(8080, "localhost", "/")
.as(BodyCodec.json(User.class))
.send(ar -> {
if (ar.succeeded()) {
HttpResponse<User> response = ar.result();
System.out.println("Got HTTP response body");
User user = response.body();
System.out.println("FirstName " + user.firstName);
System.out.println("LastName " + user.lastName);
System.out.println("Male " + user.male);
} else {
ar.cause().printStackTrace();
}
});
}
}
代码示例来源:origin: vert-x3/vertx-examples
@Override
public void start() throws Exception {
WebClient client = WebClient.create(vertx);
client.get(8080, "localhost", "/")
.as(BodyCodec.jsonObject())
.send(ar -> {
if (ar.succeeded()) {
HttpResponse<JsonObject> response = ar.result();
System.out.println("Got HTTP response body");
System.out.println(response.body().encodePrettily());
} else {
ar.cause().printStackTrace();
}
});
}
}
代码示例来源:origin: vert-x3/vertx-examples
private void invoke(RoutingContext rc) {
client.get("/luke").as(BodyCodec.string()).send(ar -> {
if (ar.failed()) {
rc.response().end("Unable to call the greeting service: " + ar.cause().getMessage());
} else {
if (ar.result().statusCode() != 200) {
rc.response().end("Unable to call the greeting service - received status:" + ar.result().statusMessage());
} else {
rc.response().end("Greeting service invoked: '" + ar.result().body() + "'");
}
}
});
}
}
代码示例来源:origin: vert-x3/vertx-examples
.as(BodyCodec.jsonObject())
.addQueryParam("grant_type", "client_credentials")
.putHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
.as(BodyCodec.jsonObject())
.addQueryParam("q", queryToSearch)
.putHeader("Authorization", header)
代码示例来源:origin: io.vertx/vertx-rx-java
/**
* Configure the request to decode the response with the <code>responseCodec</code>.
* @param responseCodec the response codec
* @return a reference to this, so the API can be used fluently
*/
public <U> io.vertx.rxjava.ext.web.client.HttpRequest<U> as(io.vertx.rxjava.ext.web.codec.BodyCodec<U> responseCodec) {
io.vertx.rxjava.ext.web.client.HttpRequest<U> ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance(delegate.as(responseCodec.getDelegate()), responseCodec.__typeArg_0);
return ret;
}
代码示例来源:origin: vert-x3/vertx-rx
/**
* Configure the request to decode the response with the <code>responseCodec</code>.
* @param responseCodec the response codec
* @return a reference to this, so the API can be used fluently
*/
public <U> io.vertx.rxjava.ext.web.client.HttpRequest<U> as(io.vertx.rxjava.ext.web.codec.BodyCodec<U> responseCodec) {
io.vertx.rxjava.ext.web.client.HttpRequest<U> ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance(delegate.as(responseCodec.getDelegate()), responseCodec.__typeArg_0);
return ret;
}
代码示例来源:origin: redhat-developer-demos/istio-tutorial
private void getNow(RoutingContext ctx) {
count++;
final WebClient client = WebClient.create(vertx);
client.get(80, HTTP_NOW, "/")
.timeout(5000)
.as(BodyCodec.jsonObject())
.send(ar -> {
if (ar.succeeded()) {
HttpResponse<JsonObject> response = ar.result();
JsonObject body = response.body();
String now = body.getJsonObject("now").getString("rfc2822");
ctx.response().end(now + " " + String.format(RESPONSE_STRING_FORMAT, HOSTNAME, count));
} else {
ctx.response().setStatusCode(503).end(ar.cause().getMessage());
}
});
}
代码示例来源:origin: EnMasseProject/enmasse
protected JsonObject getResource(String type, String basePath, String name, int expectedCode) throws Exception {
String path = basePath + "/" + name;
log.info("GET: path {}", path);
CompletableFuture<JsonObject> responsePromise = new CompletableFuture<>();
return doRequestNTimes(initRetry, () -> {
client.get(endpoint.getPort(), endpoint.getHost(), path)
.as(BodyCodec.jsonObject())
.putHeader(HttpHeaders.AUTHORIZATION, authzString)
.send(ar -> responseHandler(ar,
responsePromise,
expectedCode,
String.format("Error: get %s %s", type, name)));
return responsePromise.get(30, TimeUnit.SECONDS);
},
Optional.of(endpointSupplier),
Optional.empty());
}
代码示例来源:origin: EnMasseProject/enmasse
/**
* Get all info about messaging client (stdOut, stdErr, code, isRunning)
*
* @param uuid client id
* @return
* @throws InterruptedException
* @throws ExecutionException
* @throws TimeoutException
*/
public JsonObject getClientInfo(String uuid) throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<JsonObject> responsePromise = new CompletableFuture<>();
JsonObject request = new JsonObject();
request.put("id", uuid);
client.get(endpoint.getPort(), endpoint.getHost(), "")
.as(BodyCodec.jsonObject())
.timeout(120000)
.sendJson(request,
ar -> responseHandler(ar, responsePromise, HttpURLConnection.HTTP_OK, "Error getting messaging clients info"));
return responsePromise.get(150000, TimeUnit.SECONDS);
}
代码示例来源:origin: EnMasseProject/enmasse
/**
* Stop messaging client and get all informations about them (stdOut, stdErr, code, isRunning)
*
* @param uuid client id
* @return
* @throws InterruptedException
* @throws ExecutionException
* @throws TimeoutException
*/
public JsonObject stopClient(String uuid) throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<JsonObject> responsePromise = new CompletableFuture<>();
JsonObject request = new JsonObject();
request.put("id", uuid);
client.delete(endpoint.getPort(), endpoint.getHost(), "")
.as(BodyCodec.jsonObject())
.timeout(120000)
.sendJson(request,
ar -> responseHandler(ar, responsePromise, HttpURLConnection.HTTP_OK, "Error removing messaging clients"));
return responsePromise.get(150000, TimeUnit.SECONDS);
}
代码示例来源:origin: io.vertx/vertx-web-client
@Test
public void testResponseBodyAsAsJsonObject() throws Exception {
JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu");
server.requestHandler(req -> req.response().end(expected.encode()));
startServer();
HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
get
.as(BodyCodec.jsonObject())
.send(onSuccess(resp -> {
assertEquals(200, resp.statusCode());
assertEquals(expected, resp.body());
testComplete();
}));
await();
}
代码示例来源:origin: io.vertx/vertx-junit5
@Test
@DisplayName("Start a HTTP server, then issue a HTTP client request and check the response")
void vertx_check_http_server_response() throws InterruptedException {
Vertx vertx = Vertx.vertx();
VertxTestContext testContext = new VertxTestContext();
vertx.deployVerticle(new HttpServerVerticle(), testContext.succeeding(id -> {
WebClient client = WebClient.create(vertx);
client.get(8080, "localhost", "/")
.as(BodyCodec.string())
.send(testContext.succeeding(response -> testContext.verify(() -> {
assertThat(response.body()).isEqualTo("Plop");
testContext.completeNow();
})));
}));
assertThat(testContext.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
closeVertx(vertx);
}
代码示例来源:origin: io.vertx/vertx-web-client
@Test
public void testResponseBodyUnmarshallingError() throws Exception {
server.requestHandler(req -> req.response().end("not-json-object"));
startServer();
HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
get
.as(BodyCodec.jsonObject())
.send(onFailure(err -> {
assertTrue(err instanceof DecodeException);
testComplete();
}));
await();
}
代码示例来源:origin: io.vertx/vertx-web-client
private void handleMutateCodec(HttpContext context) {
if (context.phase() == ClientPhase.RECEIVE_RESPONSE) {
if (context.clientResponse().statusCode() == 200) {
context.request().as(BodyCodec.none());
}
}
context.next();
}
代码示例来源:origin: io.vertx/vertx-web-client
@Test
public void testResponseBodyAsAsJsonMapped() throws Exception {
JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu");
server.requestHandler(req -> req.response().end(expected.encode()));
startServer();
HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
get
.as(BodyCodec.json(WineAndCheese.class))
.send(onSuccess(resp -> {
assertEquals(200, resp.statusCode());
assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.body());
testComplete();
}));
await();
}
代码示例来源:origin: io.vertx/vertx-web-client
private <R> void testResponseMissingBody(BodyCodec<R> codec) throws Exception {
server.requestHandler(req -> req.response().setStatusCode(403).end());
startServer();
HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
get
.as(codec)
.send(onSuccess(resp -> {
assertEquals(403, resp.statusCode());
assertNull(resp.body());
testComplete();
}));
await();
}
代码示例来源:origin: io.vertx/vertx-web-client
@Test
public void testHttpResponseError() throws Exception {
server.requestHandler(req -> req.response().setChunked(true).write(Buffer.buffer("some-data")).close());
startServer();
HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
get
.as(BodyCodec.jsonObject())
.send(onFailure(err -> {
assertTrue(err instanceof VertxException);
testComplete();
}));
await();
}
代码示例来源:origin: io.vertx/vertx-web-client
@Test
public void testResponseBodyDiscarded() throws Exception {
server.requestHandler(req -> req.response().end(TestUtils.randomAlphaString(1024)));
startServer();
HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
get
.as(BodyCodec.none())
.send(onSuccess(resp -> {
assertEquals(200, resp.statusCode());
assertEquals(null, resp.body());
testComplete();
}));
await();
}
代码示例来源:origin: io.vertx/vertx-web-client
@Test
public void testResponseBodyAsAsJsonArray() throws Exception {
JsonArray expected = new JsonArray().add("cheese").add("wine");
server.requestHandler(req -> req.response().end(expected.encode()));
startServer();
HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
get
.as(BodyCodec.jsonArray())
.send(onSuccess(resp -> {
assertEquals(200, resp.statusCode());
assertEquals(expected, resp.body());
testComplete();
}));
await();
}
代码示例来源:origin: io.vertx/vertx-web-client
@Test
public void testResponseBodyAsAsJsonArrayMapped() throws Exception {
JsonArray expected = new JsonArray().add("cheese").add("wine");
server.requestHandler(req -> req.response().end(expected.encode()));
startServer();
HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
get
.as(BodyCodec.json(List.class))
.send(onSuccess(resp -> {
assertEquals(200, resp.statusCode());
assertEquals(expected.getList(), resp.body());
testComplete();
}));
await();
}
内容来源于网络,如有侵权,请联系作者删除!