org.springframework.web.reactive.function.client.WebClient.get()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(331)

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

WebClient.get介绍

[英]Start building an HTTP GET request.
[中]开始构建HTTP GET请求。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Test
public void uriBuilderWithPathOverride() {
  this.builder.build().get()
      .uri(builder -> builder.replacePath("/path").build())
      .exchange();
  ClientRequest request = verifyAndGetRequest();
  assertEquals("/path", request.url().toString());
  verifyNoMoreInteractions(this.exchangeFunction);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void switchToErrorOnEmptyClientResponseMono() {
  StepVerifier.create(builder.build().get().uri("/path").exchange())
      .expectErrorMessage("The underlying HTTP client completed without emitting a response.")
      .verify(Duration.ofSeconds(5));
}

代码示例来源:origin: spring-projects/spring-framework

@Test  // SPR-14992
public void writeAndAutoFlushBeforeComplete() {
  Mono<String> result = this.webClient.get()
      .uri("/write-and-never-complete")
      .retrieve()
      .bodyToFlux(String.class)
      .next();
  StepVerifier.create(result)
      .expectNextMatches(s -> s.startsWith("0123456789"))
      .expectComplete()
      .verify(Duration.ofSeconds(10L));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void basic() {
  this.builder.build().get().uri("/path").exchange();
  ClientRequest request = verifyAndGetRequest();
  assertEquals("/base/path", request.url().toString());
  assertEquals(new HttpHeaders(), request.headers());
  assertEquals(Collections.emptyMap(), request.cookies());
}

代码示例来源:origin: spring-projects/spring-framework

@Test  // SPR-15782
public void shouldFailWithRelativeUrls() {
  String uri = "/api/v4/groups/1";
  Mono<ClientResponse> responseMono = WebClient.builder().build().get().uri(uri).exchange();
  StepVerifier.create(responseMono)
      .expectErrorMessage("URI is not absolute: " + uri)
      .verify(Duration.ofSeconds(5));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void uriBuilder() {
  this.builder.build().get()
      .uri(builder -> builder.path("/path").queryParam("q", "12").build())
      .exchange();
  ClientRequest request = verifyAndGetRequest();
  assertEquals("/base/path?q=12", request.url().toString());
  verifyNoMoreInteractions(this.exchangeFunction);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void empty() {
  Mono<Msg> result = this.webClient.get()
      .uri("/empty")
      .retrieve()
      .bodyToMono(Msg.class);
  StepVerifier.create(result)
      .verifyComplete();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void defaultInstance() {
  Mono<Msg> result = this.webClient.get()
      .uri("/default-instance")
      .retrieve()
      .bodyToMono(Msg.class);
  StepVerifier.create(result)
      .verifyComplete();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void sseAsEventWithoutAcceptHeader() {
  Flux<ServerSentEvent<Person>> result = this.webClient.get()
      .uri("/event")
      .accept(TEXT_EVENT_STREAM)
      .retrieve()
      .bodyToFlux(new ParameterizedTypeReference<ServerSentEvent<Person>>() {});
  verifyPersonEvents(result);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void shouldReceiveEmptyResponse() {
  prepareResponse(response -> response.setHeader("Content-Length", "0").setBody(""));
  Mono<ResponseEntity<Void>> result = this.webClient.get()
      .uri("/noContent")
      .exchange()
      .flatMap(response -> response.toEntity(Void.class));
  StepVerifier.create(result).assertNext(r -> {
    assertTrue(r.getStatusCode().is2xxSuccessful());
  }).verifyComplete();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void requestHeaderAndCookie() {
  this.builder.build().get().uri("/path").accept(MediaType.APPLICATION_JSON)
      .cookies(cookies -> cookies.add("id", "123"))	// SPR-16178
      .exchange();
  ClientRequest request = verifyAndGetRequest();
  assertEquals("application/json", request.headers().getFirst("Accept"));
  assertEquals("123", request.cookies().getFirst("id"));
  verifyNoMoreInteractions(this.exchangeFunction);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void sseAsString() {
  Flux<String> result = this.webClient.get()
      .uri("/string")
      .accept(TEXT_EVENT_STREAM)
      .retrieve()
      .bodyToFlux(String.class);
  StepVerifier.create(result)
      .expectNext("foo 0")
      .expectNext("foo 1")
      .expectComplete()
      .verify(Duration.ofSeconds(5L));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void sseAsString() {
  Flux<String> result = this.webClient.get()
      .uri("/string")
      .accept(TEXT_EVENT_STREAM)
      .retrieve()
      .bodyToFlux(String.class);
  StepVerifier.create(result)
      .expectNext("foo 0")
      .expectNext("foo 1")
      .thenCancel()
      .verify(Duration.ofSeconds(5L));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void sseAsEvent() {
  Assume.assumeTrue(server instanceof JettyHttpServer);
  Flux<ServerSentEvent<Person>> result = this.webClient.get()
      .uri("/event")
      .accept(TEXT_EVENT_STREAM)
      .retrieve()
      .bodyToFlux(new ParameterizedTypeReference<ServerSentEvent<Person>>() {});
  verifyPersonEvents(result);
}

代码示例来源:origin: spring-projects/spring-framework

@Test // SPR-17482
public void bodyToMonoVoidWithoutContentType() {
  this.server.enqueue(new MockResponse()
      .setResponseCode(HttpStatus.ACCEPTED.value())
      .setChunkedBody("{\"foo\" : \"123\",  \"baz\" : \"456\", \"baz\" : \"456\"}", 5));
  Mono<Map<String, String>> mono = this.webClient.get()
      .uri("/sample").accept(MediaType.APPLICATION_JSON)
      .retrieve()
      .bodyToMono(new ParameterizedTypeReference<Map<String, String>>() {});
  StepVerifier.create(mono).expectError(UnsupportedMediaTypeException.class).verify(Duration.ofSeconds(3));
  assertEquals(1, this.server.getRequestCount());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void defaultHeaderAndCookie() {
  WebClient client = this.builder
      .defaultHeader("Accept", "application/json").defaultCookie("id", "123")
      .build();
  client.get().uri("/path").exchange();
  ClientRequest request = verifyAndGetRequest();
  assertEquals("application/json", request.headers().getFirst("Accept"));
  assertEquals("123", request.cookies().getFirst("id"));
  verifyNoMoreInteractions(this.exchangeFunction);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void apply() {
  WebClient client = this.builder
      .apply(builder -> builder
          .defaultHeader("Accept", "application/json")
          .defaultCookie("id", "123"))
      .build();
  client.get().uri("/path").exchange();
  ClientRequest request = verifyAndGetRequest();
  assertEquals("application/json", request.headers().getFirst("Accept"));
  assertEquals("123", request.cookies().getFirst("id"));
  verifyNoMoreInteractions(this.exchangeFunction);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void defaultHeaderAndCookieOverrides() {
  WebClient client = this.builder
      .defaultHeader("Accept", "application/json")
      .defaultCookie("id", "123")
      .build();
  client.get().uri("/path").header("Accept", "application/xml").cookie("id", "456").exchange();
  ClientRequest request = verifyAndGetRequest();
  assertEquals("application/xml", request.headers().getFirst("Accept"));
  assertEquals("456", request.cookies().getFirst("id"));
  verifyNoMoreInteractions(this.exchangeFunction);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void bodyToMonoVoid() {
  this.server.enqueue(new MockResponse()
      .setResponseCode(201)
      .setHeader("Content-Type", "application/json")
      .setChunkedBody("{\"foo\" : {\"bar\" : \"123\", \"baz\" : \"456\"}}", 5));
  Mono<Void> mono = this.webClient.get()
      .uri("/json").accept(MediaType.APPLICATION_JSON)
      .retrieve()
      .bodyToMono(Void.class);
  StepVerifier.create(mono).expectComplete().verify(Duration.ofSeconds(3));
  assertEquals(1, this.server.getRequestCount());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void smileStreaming() {
  Flux<Person> result = this.webClient.get()
      .uri("/stream")
      .accept(new MediaType("application", "stream+x-jackson-smile"))
      .retrieve()
      .bodyToFlux(Person.class);
  StepVerifier.create(result)
      .expectNext(new Person("foo 0"))
      .expectNext(new Person("foo 1"))
      .thenCancel()
      .verify();
}

相关文章