io.vertx.reactivex.ext.web.client.HttpResponse.body()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(88)

本文整理了Java中io.vertx.reactivex.ext.web.client.HttpResponse.body()方法的一些代码示例,展示了HttpResponse.body()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpResponse.body()方法的具体详情如下:
包路径:io.vertx.reactivex.ext.web.client.HttpResponse
类名称:HttpResponse
方法名:body

HttpResponse.body介绍

暂无

代码示例

代码示例来源:origin: vert-x3/vertx-examples

@Override
 public void start() throws Exception {

  WebClient client = WebClient.create(vertx);
  Single<HttpResponse<Data>> request = client.get(8080, "localhost", "/")
   .as(BodyCodec.json(Data.class))
   .rxSend();

  // Fire the request
  request.subscribe(resp -> System.out.println("Server content " + resp.body().message));

  // Again
  request.subscribe(resp -> System.out.println("Server content " + resp.body().message));

  // And again
  request.subscribe(resp -> System.out.println("Server content " + resp.body().message));
 }
}

代码示例来源:origin: io.gravitee.elasticsearch/gravitee-common-elasticsearch

@Override
public Single<BulkResponse> bulk(final List<String> data) {
  if (data != null && !data.isEmpty()) {
    String content = data.stream().collect(Collectors.joining());
    return httpClient
        .post(URL_BULK)
        .putHeader(HttpHeaders.CONTENT_TYPE, "application/x-ndjson")
        .rxSendBuffer(Buffer.buffer(content))
        .map(response -> {
          if (response.statusCode() != HttpStatusCode.OK_200) {
            logger.error("Unable to bulk index data: status[{}] data[{}] response[{}]",
                response.statusCode(), content, response.body());
            throw new ElasticsearchException("Unable to bulk index data");
          }
          return mapper.readValue(response.bodyAsString(), BulkResponse.class);
        });
  }
  return Single.never();
}

代码示例来源:origin: vert-x3/vertx-examples

@Override
 public void start() throws Exception {
  WebClient client = WebClient.create(vertx);
  Single<HttpResponse<String>> request = client.get(8080, "localhost", "/")
   .as(BodyCodec.string())
   .rxSend();

  // Fire the request
  request.subscribe(resp -> System.out.println("Server content " + resp.body()));

  // Again
  request.subscribe(resp -> System.out.println("Server content " + resp.body()));

  // And again
  request.subscribe(resp -> System.out.println("Server content " + resp.body()));
 }
}

代码示例来源:origin: vert-x3/vertx-examples

@Override
 public void start() throws Exception {

  // Create two requests
  WebClient client = WebClient.create(vertx);
  Single<JsonObject> request = client.get(8080, "localhost", "/")
   .as(BodyCodec.jsonObject())
   .rxSend()
   .map(resp -> resp.body());

  // Combine the responses with the zip into a single response
  request
   .zipWith(request, (b1, b2) -> new JsonObject().put("req1", b1).put("req2", b2))
   .subscribe(json -> {
    System.out.println("Got combined result " + json);
   }, err -> {
    err.printStackTrace();
   });
 }
}

代码示例来源:origin: io.knotx.acme/acme-forms-adapter-http

private Single<Buffer> toBody(HttpResponse<Buffer> response) {
 if (response.body() != null) {
  return Single.just(response.body());
 } else {
  LOGGER.warn("Service returned empty body");
  return Single.just(Buffer.buffer());
 }
}

代码示例来源:origin: io.knotx/knotx-databridge-adapter-http

private Single<Buffer> toBody(HttpResponse<Buffer> response) {
 if (response.body() != null) {
  return Single.just(response.body());
 } else {
  LOGGER.warn("Service returned empty body");
  return Single.just(Buffer.buffer());
 }
}

代码示例来源:origin: Cognifide/knotx

private Single<Buffer> toBody(HttpResponse<Buffer> response) {
 if (response.body() != null) {
  return Single.just(response.body());
 } else {
  LOGGER.warn("Service returned empty body");
  return Single.just(Buffer.buffer());
 }
}

代码示例来源:origin: nemtech/nem2-sdk-java

static JsonObject mapJsonObjectOrError(final HttpResponse<JsonObject> response) {
  if (response.statusCode() < 200 || response.statusCode() > 299) {
    throw new RuntimeException(response.statusMessage());
  }
  return response.body();
}

代码示例来源:origin: nemtech/nem2-sdk-java

static JsonArray mapJsonArrayOrError(final HttpResponse<JsonArray> response) {
    if (response.statusCode() < 200 || response.statusCode() > 299) {
      throw new RuntimeException(response.statusMessage());
    }
    return response.body();
  }
}

代码示例来源:origin: FroMage/redpipe

private Single<String> get(Vertx vertx, URI uri){
  WebClient client = WebClient.create(vertx);
  Single<HttpResponse<Buffer>> responseHandler = 
      client.get(uri.getPort(), uri.getHost(), uri.getPath()).rxSend();
  return responseHandler.map(response -> response.body().toString())
      .doAfterTerminate(() -> client.close());
}

代码示例来源:origin: FroMage/redpipe

@Path("7")
@GET
public CompletionStage<String> hello7(@Context Vertx vertx){
  io.vertx.reactivex.core.Vertx rxVertx = io.vertx.reactivex.core.Vertx.newInstance(vertx);
  System.err.println("Creating client");
  WebClientOptions options = new WebClientOptions();
  options.setSsl(true);
  options.setTrustAll(true);
  options.setVerifyHost(false);
  WebClient client = WebClient.create(rxVertx, options);
  Single<HttpResponse<io.vertx.reactivex.core.buffer.Buffer>> responseHandler = client.get(443,
      "www.google.com", 
      "/robots.txt").rxSend();
  CompletableFuture<String> ret = new CompletableFuture<>();
  responseHandler
    .doAfterTerminate(() -> client.close())
    .subscribe(body -> {
    System.err.println("Got body");
    ret.complete(body.body().toString());
  });
  
  System.err.println("Created client");
  return ret;
}

代码示例来源:origin: FroMage/redpipe

@Path("coroutines/1")
  @GET
  public Single<Response> helloAsync(@Context io.vertx.reactivex.core.Vertx rxVertx){
    return Fibers.fiber(() -> {
      System.err.println("Creating client");
      WebClientOptions options = new WebClientOptions();
      options.setSsl(true);
      options.setTrustAll(true);
      options.setVerifyHost(false);
      WebClient client = WebClient.create(rxVertx, options);
      Single<HttpResponse<io.vertx.reactivex.core.buffer.Buffer>> responseHandler = client.get(443,
          "www.google.com", 
          "/robots.txt").rxSend();

      System.err.println("Got response");

      HttpResponse<io.vertx.reactivex.core.buffer.Buffer> httpResponse = Fibers.await(responseHandler);
      System.err.println("Got body");
      client.close();
      
      return Response.ok(httpResponse.body().toString()).build();
    });
  }
}

代码示例来源:origin: FroMage/redpipe

@Path("8")
@GET
public Single<String> hello8(@Context io.vertx.reactivex.core.Vertx rxVertx){
  System.err.println("Creating client");
  WebClientOptions options = new WebClientOptions();
  options.setSsl(true);
  options.setTrustAll(true);
  options.setVerifyHost(false);
  WebClient client = WebClient.create(rxVertx, options);
  Single<HttpResponse<io.vertx.reactivex.core.buffer.Buffer>> responseHandler = client.get(443,
      "www.google.com", 
      "/robots.txt").rxSend();
  System.err.println("Created client");
  return responseHandler.map(body -> {
    System.err.println("Got body");
    return body.body().toString();
  }).doAfterTerminate(() -> client.close());
}

代码示例来源:origin: FroMage/redpipe

@Path("6")
@GET
public void hello6(@Suspended final AsyncResponse asyncResponse,
     // Inject the Vertx instance
     @Context Vertx vertx){
  io.vertx.reactivex.core.Vertx rxVertx = io.vertx.reactivex.core.Vertx.newInstance(vertx);
  System.err.println("Creating client");
  WebClientOptions options = new WebClientOptions();
  options.setSsl(true);
  options.setTrustAll(true);
  options.setVerifyHost(false);
  WebClient client = WebClient.create(rxVertx, options);
  Single<HttpResponse<io.vertx.reactivex.core.buffer.Buffer>> responseHandler = client.get(443,
      "www.google.com", 
      "/robots.txt").rxSend();
  responseHandler
    .doAfterTerminate(() -> client.close())
    .subscribe(body -> {
    System.err.println("Got body");
    asyncResponse.resume(Response.ok(body.body().toString()).build());
  });
  
  System.err.println("Created client");
}

代码示例来源:origin: FroMage/redpipe

@Path("8user")
@Produces("text/json")
@GET
public Single<DataClass> hello8User(@Context io.vertx.reactivex.core.Vertx rxVertx){
  System.err.println("Creating client");
  WebClientOptions options = new WebClientOptions();
  options.setSsl(true);
  options.setTrustAll(true);
  options.setVerifyHost(false);
  WebClient client = WebClient.create(rxVertx, options);
  Single<HttpResponse<io.vertx.reactivex.core.buffer.Buffer>> responseHandler = client.get(443,
      "www.google.com", 
      "/robots.txt").rxSend();
  System.err.println("Created client");
  return responseHandler.map(body -> {
    System.err.println("Got body");
    return new DataClass(body.body().toString());
  }).doAfterTerminate(() -> client.close());
}

代码示例来源:origin: io.gravitee.elasticsearch/gravitee-common-elasticsearch

@Override
public Completable putPipeline(String pipelineName, String pipeline) {
  return httpClient
      .put(URL_INGEST + '/' + pipelineName)
      .putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
      .rxSendBuffer(Buffer.buffer(pipeline))
      .flatMapCompletable(response -> {
        switch (response.statusCode()) {
          case HttpStatusCode.OK_200:
            return Completable.complete();
          case HttpStatusCode.BAD_REQUEST_400:
            logger.warn("Unable to create ES pipeline: {}", pipelineName);
            break;
          default:
            logger.error("Unable to put pipeline: status[{}] pipeline[{}] response[{}]",
                response.statusCode(), pipeline, response.body());
            break;
        }
        return Completable.error(new ElasticsearchException("Unable to create ES pipeline: " + pipelineName));
      });
}

代码示例来源:origin: io.gravitee.elasticsearch/gravitee-common-elasticsearch

@Override
public Completable putTemplate(String templateName, String template) {
  return httpClient
      .put(URL_TEMPLATE + '/' + templateName)
      .putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
      .rxSendBuffer(Buffer.buffer(template))
      .flatMapCompletable(response -> {
        if (response.statusCode() != HttpStatusCode.OK_200) {
          logger.error("Unable to put template mapping: status[{}] template[{}] response[{}]",
              response.statusCode(), template, response.body());
          return Completable.error(new ElasticsearchException("Unable to put template mapping"));
        }
        return Completable.complete();
      });
}

代码示例来源:origin: Cognifide/knotx

private void testGetRequest(VertxTestContext context, Vertx vertx, String url,
  String expectedResult) {
 WebClient client = WebClient.create(vertx);
 Single<HttpResponse<Buffer>> httpResponseSingle = client
   .get(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, url).rxSend();
 subscribeToResult_shouldSucceed(context, httpResponseSingle,
   resp -> {
    assertEquals(expectedResult, resp.body().toString(),
      "Wrong engines processed request, expected " + expectedResult);
    assertEquals(HttpResponseStatus.OK.code(), resp.statusCode());
    assertNotNull(resp.getHeader(EXPECTED_RESPONSE_HEADER));
    assertEquals(EXPECTED_XSERVER_HEADER_VALUE,
      resp.getHeader(EXPECTED_RESPONSE_HEADER));
   });
}

代码示例来源:origin: io.vertx/vertx-rx-java2

@Test
public void testResponseBodyAsAsJsonMapped() throws Exception {
 JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu");
 HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
 server.requestStream().handler(req -> req.response().end(expected.encode()));
 try {
  server.listen(ar -> {
   client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
   Single<HttpResponse<WineAndCheese>> single = client
       .get(8080, "localhost", "/the_uri")
       .as(BodyCodec.json(WineAndCheese.class))
       .rxSend();
   single.subscribe(resp -> {
    assertEquals(200, resp.statusCode());
    assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.body());
    testComplete();
   }, this::fail);
  });
  await();
 } finally {
  server.close();
 }
}

代码示例来源:origin: io.vertx/vertx-rx-java2

@Test
public void testResponseMissingBody() throws Exception {
 int times = 5;
 waitFor(times);
 HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
 server.requestStream().handler(req -> req.response().setStatusCode(403).end());
 try {
  server.listen(ar -> {
   client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
   Single<HttpResponse<Buffer>> single = client
       .get(8080, "localhost", "/the_uri")
       .rxSend();
   for (int i = 0; i < times; i++) {
    single.subscribe(resp -> {
     assertEquals(403, resp.statusCode());
     assertNull(resp.body());
     complete();
    }, this::fail);
   }
  });
  await();
 } finally {
  server.close();
 }
}

相关文章