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

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

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

RestTemplate.exchange介绍

暂无

代码示例

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

@Test
public void exchangeGet() throws Exception {
  HttpHeaders requestHeaders = new HttpHeaders();
  requestHeaders.set("MyHeader", "MyValue");
  HttpEntity<String> requestEntity = new HttpEntity<>(requestHeaders);
  ResponseEntity<String> response =
      template.exchange(baseUrl + "/{method}", HttpMethod.GET, requestEntity, String.class, "get");
  assertEquals("Invalid content", helloWorld, response.getBody());
}

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

<T> ResponseEntity<T> performGet(String url, MediaType out, ParameterizedTypeReference<T> type)
    throws Exception {
  HttpHeaders headers = new HttpHeaders();
  headers.setAccept(Collections.singletonList(out));
  return this.restTemplate.exchange(prepareGet(url, headers), type);
}

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

<T> ResponseEntity<T> performPost(String url, MediaType in, Object body, MediaType out,
    ParameterizedTypeReference<T> type) throws Exception {
  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(in);
  if (out != null) {
    headers.setAccept(Collections.singletonList(out));
  }
  return getRestTemplate().exchange(preparePost(url, headers, body), type);
}

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

<T> ResponseEntity<T> performPost(String url, MediaType in, Object body, MediaType out, Class<T> type)
    throws Exception {
  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(in);
  if (out != null) {
    headers.setAccept(Collections.singletonList(out));
  }
  return  getRestTemplate().exchange(preparePost(url, headers, body), type);
}

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

<T> ResponseEntity<T> performGet(String url, MediaType out, Class<T> type) throws Exception {
  HttpHeaders headers = new HttpHeaders();
  headers.setAccept(Collections.singletonList(out));
  return getRestTemplate().exchange(prepareGet(url, headers), type);
}

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

@Test
public void testRequestToFooHandler() throws Exception {
  URI url = new URI("http://localhost:" + this.port + "/foo");
  RequestEntity<Void> request = RequestEntity.get(url).build();
  ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class);
  assertEquals(HttpStatus.OK, response.getStatusCode());
  assertArrayEquals("foo".getBytes("UTF-8"), response.getBody());
}

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

@Test
public void testRequestToBarHandler() throws Exception {
  URI url = new URI("http://localhost:" + this.port + "/bar");
  RequestEntity<Void> request = RequestEntity.get(url).build();
  ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class);
  assertEquals(HttpStatus.OK, response.getStatusCode());
  assertArrayEquals("bar".getBytes("UTF-8"), response.getBody());
}

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

@Test
public void checkUri() throws Exception {
  URI url = new URI("http://localhost:" + port + "/foo?param=bar");
  RequestEntity<Void> request = RequestEntity.post(url).build();
  ResponseEntity<Void> response = new RestTemplate().exchange(request, Void.class);
  assertEquals(HttpStatus.OK, response.getStatusCode());
}

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

@Test
public void writeOnly() throws Exception {
  RestTemplate restTemplate = new RestTemplate();
  this.body = randomBytes();
  RequestEntity<byte[]> request = RequestEntity.post(
      new URI("http://localhost:" + port)).body(
          "".getBytes(StandardCharsets.UTF_8));
  ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class);
  assertArrayEquals(body, response.getBody());
}

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

@Test
@Ignore  // TODO: fragile due to socket failures
public void basicTest() throws Exception {
  URI url = new URI("http://localhost:" + port);
  ResponseEntity<String> response = new RestTemplate().exchange(
      RequestEntity.get(url).build(), String.class);
  assertThat(response.getBody(), Matchers.equalTo("hello"));
}

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

@Test
public void exchangePost() throws Exception {
  HttpHeaders requestHeaders = new HttpHeaders();
  requestHeaders.set("MyHeader", "MyValue");
  requestHeaders.setContentType(MediaType.TEXT_PLAIN);
  HttpEntity<String> entity = new HttpEntity<>(helloWorld, requestHeaders);
  HttpEntity<Void> result = template.exchange(baseUrl + "/{method}", POST, entity, Void.class, "post");
  assertEquals("Invalid location", new URI(baseUrl + "/post/1"), result.getHeaders().getLocation());
  assertFalse(result.hasBody());
}

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

@Test
public void echo() throws Exception {
  RestTemplate restTemplate = new RestTemplate();
  byte[] body = randomBytes();
  RequestEntity<byte[]> request = RequestEntity.post(new URI("http://localhost:" + port)).body(body);
  ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class);
  assertArrayEquals(body, response.getBody());
}

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

@Test
public void flux() {
  ParameterizedTypeReference<List<Person>> reference = new ParameterizedTypeReference<List<Person>>() {};
  ResponseEntity<List<Person>> result =
      restTemplate.exchange("http://localhost:" + port + "/flux", HttpMethod.GET, null, reference);
  assertEquals(HttpStatus.OK, result.getStatusCode());
  List<Person> body = result.getBody();
  assertEquals(2, body.size());
  assertEquals("John", body.get(0).getName());
  assertEquals("Jane", body.get(1).getName());
}

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

@Test
public void flux() {
  ParameterizedTypeReference<List<Person>> reference = new ParameterizedTypeReference<List<Person>>() {};
  ResponseEntity<List<Person>> result =
      this.restTemplate
          .exchange("http://localhost:" + this.port + "/flux", HttpMethod.GET, null, reference);
  assertEquals(HttpStatus.OK, result.getStatusCode());
  List<Person> body = result.getBody();
  assertEquals(2, body.size());
  assertEquals("John", body.get(0).getName());
  assertEquals("Jane", body.get(1).getName());
}

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

@Test
public void getFormParts() throws Exception {
  RestTemplate restTemplate = new RestTemplate();
  RequestEntity<MultiValueMap<String, Object>> request = RequestEntity
      .post(new URI("http://localhost:" + port + "/form-parts"))
      .contentType(MediaType.MULTIPART_FORM_DATA)
      .body(generateBody());
  ResponseEntity<Void> response = restTemplate.exchange(request, Void.class);
  assertEquals(HttpStatus.OK, response.getStatusCode());
}

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

@Test
public void testRequestToHeaderSettingHandler() throws Exception {
  URI url = new URI("http://localhost:" + this.port + "/header");
  RequestEntity<Void> request = RequestEntity.get(url).build();
  ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class);
  assertEquals(HttpStatus.OK, response.getStatusCode());
  assertEquals("bar", response.getHeaders().getFirst("foo"));
}

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

@Test
public void postMono() {
  URI uri = URI.create("http://localhost:" + port + "/mono");
  Person person = new Person("Jack");
  RequestEntity<Person> requestEntity = RequestEntity.post(uri).body(person);
  ResponseEntity<Person> result = restTemplate.exchange(requestEntity, Person.class);
  assertEquals(HttpStatus.OK, result.getStatusCode());
  assertEquals("Jack", result.getBody().getName());
}

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

@Test
public void etagCheckWithNotModifiedResponse() throws Exception {
  URI uri = new URI("http://localhost:" + this.port + "/html");
  RequestEntity<Void> request = RequestEntity.get(uri).ifNoneMatch("\"deadb33f8badf00d\"").build();
  ResponseEntity<String> response = getRestTemplate().exchange(request, String.class);
  assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode());
  assertNull(response.getBody());
}

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

@Test
public void forwardedHeaders() {
  // One integration test to verify triggering of Forwarded header support.
  // More fine-grained tests in ForwardedHeaderTransformerTests.
  RequestEntity<Void> request = RequestEntity
      .get(URI.create("http://localhost:" + this.port + "/uri"))
      .header("Forwarded", "host=84.198.58.199;proto=https")
      .build();
  ResponseEntity<String> entity = getRestTemplate().exchange(request, String.class);
  assertEquals("https://84.198.58.199/uri", entity.getBody());
}

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

@Test
public void random() throws Throwable {
  // TODO: fix Reactor support
  RestTemplate restTemplate = new RestTemplate();
  byte[] body = randomBytes();
  RequestEntity<byte[]> request = RequestEntity.post(new URI("http://localhost:" + port)).body(body);
  ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class);
  assertNotNull(response.getBody());
  assertEquals(RESPONSE_SIZE,
      response.getHeaders().getContentLength());
  assertEquals(RESPONSE_SIZE, response.getBody().length);
}

相关文章

微信公众号

最新文章

更多