org.springframework.web.client.RestTemplate.execute()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(518)

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

RestTemplate.execute介绍

[英]To provide a RequestCallback or ResponseExtractor only, but not both, consider using:

  • #acceptHeaderRequestCallback(Class)
  • #httpEntityCallback(Object)
  • #httpEntityCallback(Object,Type)
  • #responseEntityExtractor(Type)
    [中]只提供请求回调或响应提取器,但不能同时使用,请考虑使用:
    *#acceptHeaderRequestCallback(类)
    *#httpEntityCallback(对象)
    *#httpEntityCallback(对象、类型)
    *#responseEntityExtractor(类型)

代码示例

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

@Override
public void put(String url, @Nullable Object request, Map<String, ?> uriVariables)
    throws RestClientException {
  RequestCallback requestCallback = httpEntityCallback(request);
  execute(url, HttpMethod.PUT, requestCallback, null, uriVariables);
}

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

@Override
public void put(URI url, @Nullable Object request) throws RestClientException {
  RequestCallback requestCallback = httpEntityCallback(request);
  execute(url, HttpMethod.PUT, requestCallback, null);
}

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

@Override
public Set<HttpMethod> optionsForAllow(String url, Map<String, ?> uriVariables) throws RestClientException {
  ResponseExtractor<HttpHeaders> headersExtractor = headersExtractor();
  HttpHeaders headers = execute(url, HttpMethod.OPTIONS, null, headersExtractor, uriVariables);
  return (headers != null ? headers.getAllow() : Collections.emptySet());
}

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

@Override
public Set<HttpMethod> optionsForAllow(URI url) throws RestClientException {
  ResponseExtractor<HttpHeaders> headersExtractor = headersExtractor();
  HttpHeaders headers = execute(url, HttpMethod.OPTIONS, null, headersExtractor);
  return (headers != null ? headers.getAllow() : Collections.emptySet());
}

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

@Override
@Nullable
public URI postForLocation(String url, @Nullable Object request, Map<String, ?> uriVariables)
    throws RestClientException {
  RequestCallback requestCallback = httpEntityCallback(request);
  HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, headersExtractor(), uriVariables);
  return (headers != null ? headers.getLocation() : null);
}

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

@Override
@Nullable
public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType,
    Map<String, ?> uriVariables) throws RestClientException {
  RequestCallback requestCallback = httpEntityCallback(request, responseType);
  HttpMessageConverterExtractor<T> responseExtractor =
      new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
  return execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables);
}

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

@Override
@Nullable
public <T> T patchForObject(String url, @Nullable Object request, Class<T> responseType,
    Object... uriVariables) throws RestClientException {
  RequestCallback requestCallback = httpEntityCallback(request, responseType);
  HttpMessageConverterExtractor<T> responseExtractor =
      new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
  return execute(url, HttpMethod.PATCH, requestCallback, responseExtractor, uriVariables);
}

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

@Override
@Nullable
public URI postForLocation(URI url, @Nullable Object request) throws RestClientException {
  RequestCallback requestCallback = httpEntityCallback(request);
  HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, headersExtractor());
  return (headers != null ? headers.getLocation() : null);
}

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

@Override
@Nullable
public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType)
    throws RestClientException {
  RequestCallback requestCallback = httpEntityCallback(request, responseType);
  HttpMessageConverterExtractor<T> responseExtractor =
      new HttpMessageConverterExtractor<>(responseType, getMessageConverters());
  return execute(url, HttpMethod.POST, requestCallback, responseExtractor);
}

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

@Override
@Nullable
public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException {
  RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
  HttpMessageConverterExtractor<T> responseExtractor =
      new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
  return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
}

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

@Override
public <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request,
    Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
  RequestCallback requestCallback = httpEntityCallback(request, responseType);
  ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
  return nonNull(execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables));
}

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

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

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

@Override
@Nullable
public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException {
  RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
  HttpMessageConverterExtractor<T> responseExtractor =
      new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
  return execute(url, HttpMethod.GET, requestCallback, responseExtractor);
}

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

@Override
public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity,
    Class<T> responseType) throws RestClientException {
  RequestCallback requestCallback = httpEntityCallback(requestEntity, responseType);
  ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
  return nonNull(execute(url, method, requestCallback, responseExtractor));
}

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

@Test  // SPR-15201
public void uriTemplateWithTrailingSlash() throws Exception {
  String url = "http://example.com/spring/";
  mockSentRequest(GET, url);
  mockResponseStatus(HttpStatus.OK);
  template.execute(url, GET, null, null);
  verify(response).close();
}

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

@Test
public void varArgsTemplateVariables() throws Exception {
  mockSentRequest(GET, "http://example.com/hotels/42/bookings/21");
  mockResponseStatus(HttpStatus.OK);
  template.execute("http://example.com/hotels/{hotel}/bookings/{booking}", GET,
      null, null, "42", "21");
  verify(response).close();
}

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

@Test
public void varArgsNullTemplateVariable() throws Exception {
  mockSentRequest(GET, "http://example.com/-foo");
  mockResponseStatus(HttpStatus.OK);
  template.execute("http://example.com/{first}-{last}", GET, null, null, null, "foo");
  verify(response).close();
}

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

@Test
public void badRequest() {
  try {
    template.execute(baseUrl + "/status/badrequest", HttpMethod.GET, null, null);
    fail("HttpClientErrorException.BadRequest expected");
  }
  catch (HttpClientErrorException.BadRequest ex) {
    assertEquals(HttpStatus.BAD_REQUEST, ex.getStatusCode());
    assertEquals("400 Client Error", ex.getMessage());
  }
}

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

@Test
public void notFound() {
  try {
    template.execute(baseUrl + "/status/notfound", HttpMethod.GET, null, null);
    fail("HttpClientErrorException expected");
  }
  catch (HttpClientErrorException ex) {
    assertEquals(HttpStatus.NOT_FOUND, ex.getStatusCode());
    assertNotNull(ex.getStatusText());
    assertNotNull(ex.getResponseBodyAsString());
  }
}

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

@Test
public void serverError() {
  try {
    template.execute(baseUrl + "/status/server", HttpMethod.GET, null, null);
    fail("HttpServerErrorException expected");
  }
  catch (HttpServerErrorException ex) {
    assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, ex.getStatusCode());
    assertNotNull(ex.getStatusText());
    assertNotNull(ex.getResponseBodyAsString());
  }
}

相关文章