io.micronaut.http.HttpRequest.GET()方法的使用及代码示例

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

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

HttpRequest.GET介绍

[英]Return a MutableHttpRequest for a HttpMethod#GET request for the given URI.
[中]返回给定URI的HttpMethod#GET请求的可变HttpRequest。

代码示例

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * Return a {@link MutableHttpRequest} for a {@link HttpMethod#GET} request for the given URI.
 *
 * @param uri The URI
 * @param <T> The Http request type
 * @return The {@link MutableHttpRequest} instance
 * @see HttpRequestFactory
 */
static <T> MutableHttpRequest<T> GET(URI uri) {
  return GET(uri.toString());
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * Perform a GET request for the given request object emitting the full HTTP response from returned
 * {@link org.reactivestreams.Publisher}.
 *
 * @param uri The URI of the GET request
 * @param <O> The response body type
 * @return The full {@link HttpResponse} object
 */
default <O> HttpResponse<O> exchange(String uri) {
  return exchange(HttpRequest.GET(uri), (Argument<O>) null);
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * Perform an HTTP GET request for the given request object emitting the full HTTP response from returned
 * {@link org.reactivestreams.Publisher} and converting the response body to the specified type.
 *
 * @param uri The URI
 * @param bodyType The body type
 * @param <O> The body generic type
 * @return A result or null if a 404 is returned
 * @throws HttpClientResponseException if an error status is returned
 */
default <O> O retrieve(String uri, Class<O> bodyType) {
  return retrieve(HttpRequest.GET(uri), bodyType);
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * Perform an HTTP GET request for the given request object emitting the full HTTP response from returned
 * {@link Publisher}.
 *
 * @param uri The Uri
 * @return A {@link Publisher} that emits the full {@link HttpResponse} object
 */
default Publisher<HttpResponse<ByteBuffer>> exchange(String uri) {
  return exchange(HttpRequest.GET(uri), ByteBuffer.class);
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * Perform an HTTP GET request for the given request object emitting the full HTTP response from returned
 * {@link Publisher} and converting the response body to the specified type.
 *
 * @param uri The URI
 * @return A {@link Publisher} that emits String result
 */
default Publisher<String> retrieve(String uri) {
  return retrieve(HttpRequest.GET(uri), String.class);
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * Perform an HTTP GET request for the given request object emitting the full HTTP response from returned
 * {@link org.reactivestreams.Publisher} and converting the response body to the specified type.
 *
 * @param uri The URI
 * @return A string result or null if a 404 is returned
 * @throws HttpClientResponseException if an error status is returned
 */
default String retrieve(String uri) {
  return retrieve(HttpRequest.GET(uri), String.class);
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
   * <p>Perform an HTTP GET request and receive data as a stream of SSE {@link Event} objects as they become available without blocking.</p>
   * <p>
   * <p>The downstream {@link org.reactivestreams.Subscriber} can regulate demand via the subscription</p>
   *
   * @param uri The request URI
   * @param eventType The event data type
   * @param <B> The event body type
   * @return A {@link Publisher} that emits an {@link Event} with the data represented by the eventType argument
   */
  default <B> Publisher<Event<B>> eventStream(String uri, Argument<B> eventType) {
    return eventStream(HttpRequest.GET(uri), eventType);
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * Perform an HTTP GET request for the given request object emitting the full HTTP response from returned
 * {@link Publisher}.
 *
 * @param uri      The request URI
 * @param bodyType The body type
 * @param <O>      The response body type
 * @return A {@link Publisher} that emits the full {@link HttpResponse} object
 */
default <O> Publisher<HttpResponse<O>> exchange(String uri, Class<O> bodyType) {
  return exchange(HttpRequest.GET(uri), Argument.of(bodyType));
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * Perform a GET request for the given request object emitting the full HTTP response from returned
 * {@link org.reactivestreams.Publisher}.
 *
 * @param uri      The URI of the GET request
 * @param <O>      The response body type
 * @param bodyType The body type
 * @return The full {@link HttpResponse} object
 */
default <O> HttpResponse<O> exchange(String uri, Class<O> bodyType) {
  return exchange(HttpRequest.GET(uri), Argument.of(bodyType));
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * <p>Perform an HTTP GET request and receive data as a stream of SSE {@link Event} objects as they become available without blocking.</p>
 * <p>
 * <p>The downstream {@link org.reactivestreams.Subscriber} can regulate demand via the subscription</p>
 *
 * @param uri The request URI
 * @param eventType The event data type
 * @param <B> The event body type
 * @return A {@link Publisher} that emits an {@link Event} with the data represented by the eventType argument
 */
default <B> Publisher<Event<B>> eventStream(String uri, Class<B> eventType) {
  return eventStream(HttpRequest.GET(uri), Argument.of(eventType));
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * Perform a GET request for the given request object emitting the full HTTP response from returned
 * {@link org.reactivestreams.Publisher}.
 *
 * @param uri      The URI of the GET request
 * @param <O>      The response body type
 * @param <E>      The error type
 * @param bodyType The body type
 * @param errorType The error type
 * @return The full {@link HttpResponse} object
 */
default <O, E> O retrieve(String uri, Class<O> bodyType, Class<E> errorType) {
  return retrieve(HttpRequest.GET(uri), Argument.of(bodyType), Argument.of(errorType));
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
   * Perform a GET request for the given request object emitting the full HTTP response from returned
   * {@link org.reactivestreams.Publisher}.
   *
   * @param uri      The URI of the GET request
   * @param <O>      The response body type
   * @param <E>      The error type
   * @param bodyType The body type
   * @param errorType The error type
   * @return The full {@link HttpResponse} object
   */
  default <O, E> HttpResponse<O> exchange(String uri, Class<O> bodyType, Class<E> errorType) {
    return exchange(HttpRequest.GET(uri), Argument.of(bodyType), Argument.of(errorType));
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
  public void testReadValueFromSpringCloudConfig() throws Exception {
    HttpRequest request = HttpRequest.GET("/spring-cloud/issues/1");
    String body = client.toBlocking().retrieve(request);
    assertNotNull(body);
    assertThat(body, equalTo("test: issue # 1!"));
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Override
public <T extends AutoCloseable> Flowable<T> connect(Class<T> clientEndpointType, Map<String, Object> parameters) {
  WebSocketBean<T> webSocketBean = webSocketRegistry.getWebSocket(clientEndpointType);
  String uri = webSocketBean.getBeanDefinition().getValue(ClientWebSocket.class, String.class).orElse("/ws");
  uri = UriTemplate.of(uri).expand(parameters);
  MutableHttpRequest<Object> request = io.micronaut.http.HttpRequest.GET(uri);
  Publisher<URI> uriPublisher = resolveRequestURI(request);
  return Flowable.fromPublisher(uriPublisher)
      .switchMap((resolvedURI) -> connectWebSocket(resolvedURI, request, clientEndpointType, webSocketBean));
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Get("/hello/{name}")
Maybe<String> hello(String name) { // <1>
  return httpClient.retrieve( GET("/hello/" + name) )
           .firstElement(); // <2>
}
// end::nonblocking[]

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
  public void testGetPojoList() {
    ApplicationContext applicationContext = ApplicationContext.run();
    EmbeddedServer server = applicationContext.getBean(EmbeddedServer.class).start();

    HttpClient client = applicationContext.createBean(HttpClient.class, server.getURL());

    Flowable<HttpResponse<List>> flowable = Flowable.fromPublisher(client.exchange(
        HttpRequest.GET("/get/pojoList"), Argument.of(List.class, HttpGetSpec.Book.class)
    ));
    HttpResponse<List> response = flowable.blockingFirst();

    assertEquals(response.getStatus(), HttpStatus.OK);
    Optional<List> body = response.getBody();
    assertTrue(body.isPresent());

    List<HttpGetSpec.Book> list = body.get();
    assertEquals(list.size(), 1);
    assertTrue(list.get(0) instanceof HttpGetSpec.Book);

    client.stop();
    applicationContext.stop();
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testSimpleGet() {
  ApplicationContext applicationContext = ApplicationContext.run();
  EmbeddedServer server = applicationContext.getBean(EmbeddedServer.class).start();
  HttpClient client = new DefaultHttpClient(server.getURL());
  Flowable<HttpResponse<String>> flowable = Flowable.fromPublisher(client.exchange(
      HttpRequest.GET("/get/simple"), String.class
  ));
  HttpResponse<String> response = flowable.blockingFirst();
  Assert.assertEquals(response.getStatus(), HttpStatus.OK);
  Optional<String> body = response.getBody(String.class);
  assertTrue(body.isPresent());
  assertEquals(body.get(), "success");
  client.stop();
  applicationContext.stop();
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testRetrieveWithPOJOResponse() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());
  // tag::pojoresponse[]
  Flowable<HttpResponse<Message>> call = client.exchange(
      GET("/greet/John"), Message.class // <1>
  );
  HttpResponse<Message> response = call.blockingFirst();
  Optional<Message> message = response.getBody(Message.class); // <2>
  // check the status
  assertEquals(
      HttpStatus.OK,
      response.getStatus() // <3>
  );
  // check the body
  assertTrue(message.isPresent());
  assertEquals(
      "Hello John",
      message.get().getText()
  );
  // end::pojoresponse[]
  embeddedServer.stop();
  client.stop();
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testRetrieveWithPOJO() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());
  // tag::jsonpojo[]
  Flowable<Message> response = client.retrieve(
      GET("/greet/John"), Message.class
  );
  assertEquals(
      "Hello John",
      response.blockingFirst().getText()
  );
  // end::jsonpojo[]
  embeddedServer.stop();
  client.stop();
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testRetrieveWithHeaders() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());
  // tag::headers[]
  Flowable<String> response = client.retrieve(
      GET("/hello/John")
      .header("X-My-Header", "SomeValue")
  );
  // end::headers[]
  assertEquals(
      "Hello John",
      response.blockingFirst()
  );
  embeddedServer.stop();
  client.stop();
}

相关文章