org.springframework.test.web.client.MockRestServiceServer.createServer()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(165)

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

MockRestServiceServer.createServer介绍

[英]Spring's central class for asynchronous client-side HTTP access. Exposes similar methods as RestTemplate, but returns ListenableFuturewrappers as opposed to concrete results.

The AsyncRestTemplate exposes a synchronous RestTemplate via the #getRestOperations() method and shares its #setErrorHandlerand #setMessageConverters with that RestTemplate.

Note: by default AsyncRestTemplate relies on standard JDK facilities to establish HTTP connections. You can switch to use a different HTTP library such as Apache HttpComponents, Netty, and OkHttp by using a constructor accepting an org.springframework.http.client.AsyncClientHttpRequestFactory.

For more information, please refer to the RestTemplate API documentation.
[中]Spring的异步客户端HTTP访问中心类。公开与RestTemplate类似的方法,但返回ListenableFuturewrappers,而不是具体结果。
AsyncRestTemplate通过#getRestOperations()方法公开同步RestTemplate,并与该RestTemplate共享其#setErrorHandlerand#SetMessageConverter。
注意:默认情况下,AsyncRestTemplate依赖标准JDK工具来建立HTTP连接。通过使用接受组织的构造函数,可以切换到使用不同的HTTP库,例如Apache HttpComponents、Netty和OkHttp。springframework。http。客户AsyncClientHttpRequestFactory。
有关更多信息,请参阅RestTemplate API文档。

代码示例

代码示例来源:origin: openzipkin/brave

@Override protected AsyncRestTemplate newClient(HttpTracing httpTracing) {
 OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(ok);
 AsyncRestTemplate result = new AsyncRestTemplate(factory);
 result.setInterceptors(Collections.singletonList(
   TracingAsyncClientHttpRequestInterceptor.create(httpTracing
   )));
 return result;
}

代码示例来源:origin: aol/micro-server

public <T> CompletableFuture<ResponseEntity<T>> exchange(String url,
    HttpMethod method, HttpEntity<?> requestEntity,
    ParameterizedTypeReference<T> responseType, Object... uriVariables)
    throws RestClientException {
  return toCompletableFuture(template.exchange(url, method,
      requestEntity, responseType, uriVariables));
}

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

@Test
public void multipleFutureGets() throws Exception {
  Future<ResponseEntity<String>> future = template.getForEntity(baseUrl + "/{method}", String.class, "get");
  future.get();
  future.get();
}

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

@Override
public <T> ListenableFuture<ResponseEntity<T>> getForEntity(URI url, Class<T> responseType)
    throws RestClientException {
  AsyncRequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
  ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
  return execute(url, HttpMethod.GET, requestCallback, responseExtractor);
}

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

@Override
public ListenableFuture<HttpHeaders> headForHeaders(String url, Object... uriVariables)
    throws RestClientException {
  ResponseExtractor<HttpHeaders> headersExtractor = headersExtractor();
  return execute(url, HttpMethod.HEAD, null, headersExtractor, uriVariables);
}

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

@Override
public ListenableFuture<URI> postForLocation(String url, @Nullable HttpEntity<?> request, Object... uriVars)
    throws RestClientException {
  AsyncRequestCallback callback = httpEntityCallback(request);
  ResponseExtractor<HttpHeaders> extractor = headersExtractor();
  ListenableFuture<HttpHeaders> future = execute(url, HttpMethod.POST, callback, extractor, uriVars);
  return adaptToLocationHeader(future);
}

代码示例来源:origin: openzipkin/brave

@Override protected AsyncRestTemplate newClient() {
 return new AsyncRestTemplate(new OkHttp3ClientHttpRequestFactory(ok));
}

代码示例来源:origin: Nike-Inc/wingtips

/**
 * @param surroundCallsWithSubspan Pass in true to have the returned {@link AsyncRestTemplate} surround all calls
 * with a subspan and propagate the subspan's tracing info, or false to have only the current span propagated at
 * the time of the call (no subspan).
 * @return A new {@link AsyncRestTemplate} instance with a {@link WingtipsAsyncClientHttpRequestInterceptor}
 * already added and with the subspan option on or off depending on the value of the {@code
 * surroundCallsWithSubspan} argument, and using the default {@link HttpTagAndSpanNamingStrategy} and
 * {@link HttpTagAndSpanNamingAdapter} ({@link ZipkinHttpTagStrategy} and {@link SpringHttpClientTagAdapter}).
 */
public static AsyncRestTemplate createTracingEnabledAsyncRestTemplate(boolean surroundCallsWithSubspan) {
  AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate();
  asyncRestTemplate.getInterceptors().add(
    new WingtipsAsyncClientHttpRequestInterceptor(surroundCallsWithSubspan)
  );
  return asyncRestTemplate;
}

代码示例来源:origin: com.orange.cepheus/cepheus-ngsi

@Bean
@Resource(name = "jsonV1Converter")
public AsyncRestTemplate asyncRestTemplate(AsyncClientHttpRequestFactory asyncClientHttpRequestFactory,
    MappingJackson2HttpMessageConverter jsonConverter) {
  AsyncRestTemplate restTemplate = new AsyncRestTemplate(asyncClientHttpRequestFactory);
  // Replace the default json converter by our converter
  // Remove
  for(HttpMessageConverter httpMessageConverter : restTemplate.getMessageConverters()) {
    if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter) {
      restTemplate.getMessageConverters().remove(httpMessageConverter);
      break;
    }
  }
  // Add
  restTemplate.getMessageConverters().add(jsonConverter);
  return restTemplate;
}

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

@Override
public <T> ListenableFuture<ResponseEntity<T>> exchange(String url, HttpMethod method,
    @Nullable HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables)
    throws RestClientException {
  AsyncRequestCallback requestCallback = httpEntityCallback(requestEntity, responseType);
  ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
  return execute(url, method, requestCallback, responseExtractor, uriVariables);
}

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

@Test
@SuppressWarnings("deprecation")
public void testAsyncTemplate() throws Exception {
  org.springframework.web.client.AsyncRestTemplate template = new org.springframework.web.client.AsyncRestTemplate(
      new MockMvcClientHttpRequestFactory(this.mockMvc));
  ListenableFuture<ResponseEntity<String>> entity = template.getForEntity("/foo", String.class);
  assertEquals("bar", entity.get().getBody());
}

代码示例来源:origin: aol/micro-server

public <T> CompletableFuture<ResponseEntity<T>> postForEntity(String url,
    HttpEntity<?> request, Class<T> responseType,
    Object... uriVariables) throws RestClientException {
  return toCompletableFuture(template.postForEntity(url, request,
      responseType, uriVariables));
}

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

@Test(expected=SystemException.class)
public void bug() throws Exception {
  AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate();
  HttpHeaders headers = new HttpHeaders();
  String authHeader = "Basic " + new String(Base64.getEncoder().encode(("user:secret").getBytes()));
  headers.add(AUTHORIZATION, authHeader);
  ListenableFuture<ResponseEntity<String>> future =
    asyncRestTemplate.exchange("http://localhost:8080/sleepingendpoint", GET, new HttpEntity<String>(headers), String.class);
  future.addCallback(s -> {}, f -> {
    System.err.println("BUG -> SystemException not propaging up the chain <- BUG");
    throw new SystemException(f);	// <-- this fails in executor thread
  });
  ResponseEntity<String> resp = future.get();    // <-- this fails with ExecutionException
  assertTrue(resp.getStatusCode().is2xxSuccessful());
}

代码示例来源:origin: spring-cloud/spring-cloud-sleuth

@PostConstruct
public void init() {
  if (this.restTemplates != null) {
    for (AsyncRestTemplate restTemplate : this.restTemplates) {
      List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>(
          restTemplate.getInterceptors());
      interceptors.add(this.clientInterceptor);
      restTemplate.setInterceptors(interceptors);
    }
  }
}

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

@Test
public void getAndInterceptError() throws Exception {
  RequestInterceptor interceptor = new RequestInterceptor();
  template.setInterceptors(Collections.singletonList(interceptor));
  template.getForEntity(baseUrl + "/status/notfound", String.class);
  interceptor.latch.await(5, TimeUnit.SECONDS);
  assertNotNull(interceptor.response);
  assertEquals(HttpStatus.NOT_FOUND, interceptor.response.getStatusCode());
  assertNull(interceptor.exception);
}

代码示例来源:origin: io.micrometer/micrometer-spring-legacy

public void customize(final AsyncRestTemplate restTemplate) {
    UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
    templateHandler = this.interceptor.createUriTemplateHandler(templateHandler);
    restTemplate.setUriTemplateHandler(templateHandler);
    List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>();
    interceptors.add(this.interceptor);
    interceptors.addAll(restTemplate.getInterceptors());
    restTemplate.setInterceptors(interceptors);
  }
}

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

@Override
public ListenableFuture<?> put(String url, @Nullable HttpEntity<?> request, Map<String, ?> uriVars)
    throws RestClientException {
  AsyncRequestCallback requestCallback = httpEntityCallback(request);
  return execute(url, HttpMethod.PUT, requestCallback, null, uriVars);
}

代码示例来源:origin: org.zalando/riptide

private <T> AsyncDispatcher execute(final HttpMethod method, final URI url, final HttpEntity<T> entity) {
  final List<HttpMessageConverter<?>> converters = template.getMessageConverters();
  final Callback<T> callback = new Callback<>(converters, entity);
  final ListenableFuture<ClientHttpResponse> future = template.execute(url, method,
      new AsyncRequestCallbackAdapter<>(callback), BufferingClientHttpResponse::buffer);
  return new AsyncDispatcher(converters, future, router);
}

代码示例来源:origin: spring-cloud/spring-cloud-commons

private void assertLoadBalanced(AsyncRestTemplate restTemplate) {
  List<AsyncClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
  MatcherAssert.assertThat(interceptors, hasSize(1));
  AsyncClientHttpRequestInterceptor interceptor = interceptors.get(0);
  MatcherAssert.assertThat(interceptor, is(instanceOf(AsyncLoadBalancerInterceptor.class)));
}

代码示例来源:origin: aol/micro-server

public NIORestClient(AsyncRestTemplate template) {
  super();
  this.template = template;
  MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  converter.setObjectMapper(JacksonUtil.getMapper());
  template.getMessageConverters().add(converter);
}

相关文章