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

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

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

RestTemplate.getForEntity介绍

暂无

代码示例

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

@Test
public void attributes() {
  ResponseEntity<String> result =
      this.restTemplate
          .getForEntity("http://localhost:" + this.port + "/attributes/bar", String.class);
  assertEquals(HttpStatus.OK, result.getStatusCode());
}

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

@Test
public void responseBodyError() throws Exception {
  RestTemplate restTemplate = new RestTemplate();
  restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER);
  URI url = new URI("http://localhost:" + port + "/response-body-error");
  ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
  assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
}

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

@Test
public void handlingError() throws Exception {
  RestTemplate restTemplate = new RestTemplate();
  restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER);
  URI url = new URI("http://localhost:" + port + "/handling-error");
  ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
  assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
}

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

@Test // SPR-15560
public void emptyPathSegments() throws Exception {
  RestTemplate restTemplate = new RestTemplate();
  restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER);
  URI url = new URI("http://localhost:" + port + "//");
  ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
  assertEquals(HttpStatus.OK, response.getStatusCode());
}

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

@Test
public void bar() {
  ResponseEntity<String> result =
      restTemplate.getForEntity("http://localhost:" + port + "/foo/bar", String.class);
  assertEquals(HttpStatus.OK, result.getStatusCode());
  assertEquals("/foo/bar", result.getBody());
}

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

@Test
public void parentVariables() {
  ResponseEntity<String> result =
      restTemplate.getForEntity("http://localhost:" + port + "/1/bar", String.class);
  assertEquals(HttpStatus.OK, result.getStatusCode());
  assertEquals("/{foo}/bar\n{foo=1}", result.getBody());
}

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

@Test
public void variables() {
  ResponseEntity<String> result =
      restTemplate.getForEntity("http://localhost:" + port + "/1/2/3", String.class);
  assertEquals(HttpStatus.OK, result.getStatusCode());
  assertEquals("/{foo}/{bar}/{baz}\n{foo=1, bar=2, baz=3}", result.getBody());
}

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

@Test
public void removeFailedNestedPathVariables() {
  ResponseEntity<String> result =
      restTemplate.getForEntity("http://localhost:" + port + "/qux/quux", String.class);
  assertEquals(HttpStatus.OK, result.getStatusCode());
  assertEquals("/{qux}/quux\n{qux=qux}", result.getBody());
}

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

@Test
public void baz() {
  ResponseEntity<String> result =
      restTemplate.getForEntity("http://localhost:" + port + "/foo/baz", String.class);
  assertEquals(HttpStatus.OK, result.getStatusCode());
  assertEquals("/foo/baz", result.getBody());
}

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

@Test
public void getNoContent() {
  String s = template.getForObject(baseUrl + "/status/nocontent", String.class);
  assertNull("Invalid content", s);
  ResponseEntity<String> entity = template.getForEntity(baseUrl + "/status/nocontent", String.class);
  assertEquals("Invalid response code", HttpStatus.NO_CONTENT, entity.getStatusCode());
  assertNull("Invalid content", entity.getBody());
}

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

@Test
public void getNotModified() {
  String s = template.getForObject(baseUrl + "/status/notmodified", String.class);
  assertNull("Invalid content", s);
  ResponseEntity<String> entity = template.getForEntity(baseUrl + "/status/notmodified", String.class);
  assertEquals("Invalid response code", HttpStatus.NOT_MODIFIED, entity.getStatusCode());
  assertNull("Invalid content", entity.getBody());
}

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

@Test
public void normal() {
  ResponseEntity<String> result =
      restTemplate.getForEntity("http://localhost:" + port + "/normal", String.class);
  assertEquals(HttpStatus.OK, result.getStatusCode());
  Map<String, String> body = parseBody(result.getBody());
  assertEquals(2, body.size());
  assertEquals("foo", body.get("name"));
  assertEquals("baz", body.get("bar"));
}

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

@Test
public void mono() {
  ResponseEntity<Person> result =
      restTemplate.getForEntity("http://localhost:" + port + "/mono", Person.class);
  assertEquals(HttpStatus.OK, result.getStatusCode());
  assertEquals("John", result.getBody().getName());
}

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

@Test
public void filter() {
  ResponseEntity<String> result =
      restTemplate.getForEntity("http://localhost:" + port + "/filter", String.class);
  assertEquals(HttpStatus.OK, result.getStatusCode());
  Map<String, String> body = parseBody(result.getBody());
  assertEquals(3, body.size());
  assertEquals("foo", body.get("name"));
  assertEquals("baz", body.get("bar"));
  assertEquals("quux", body.get("qux"));
}

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

@Test
public void mono() {
  ResponseEntity<Person> result =
      this.restTemplate.getForEntity("http://localhost:" + this.port + "/mono", Person.class);
  assertEquals(HttpStatus.OK, result.getStatusCode());
  assertEquals("John", result.getBody().getName());
}

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

@Test
public void controller() {
  ResponseEntity<Person> result =
      this.restTemplate.getForEntity("http://localhost:" + this.port + "/controller", Person.class);
  assertEquals(HttpStatus.OK, result.getStatusCode());
  assertEquals("John", result.getBody().getName());
}

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

@Test
public void nested() {
  ResponseEntity<String> result = this.restTemplate
      .getForEntity("http://localhost:" + this.port + "/foo/bar", String.class);
  assertEquals(200, result.getStatusCodeValue());
}

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

@Test
public void getEntity() {
  ResponseEntity<String> entity = template.getForEntity(baseUrl + "/{method}", String.class, "get");
  assertEquals("Invalid content", helloWorld, entity.getBody());
  assertFalse("No headers", entity.getHeaders().isEmpty());
  assertEquals("Invalid content-type", textContentType, entity.getHeaders().getContentType());
  assertEquals("Invalid status code", HttpStatus.OK, entity.getStatusCode());
}

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

@Test
public void getForEntity() throws Exception {
  HttpHeaders requestHeaders = new HttpHeaders();
  mockSentRequest(GET, "http://example.com", requestHeaders);
  mockTextPlainHttpMessageConverter();
  mockResponseStatus(HttpStatus.OK);
  String expected = "Hello World";
  mockTextResponseBody(expected);
  ResponseEntity<String> result = template.getForEntity("http://example.com", String.class);
  assertEquals("Invalid GET result", expected, result.getBody());
  assertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
  assertEquals("Invalid Content-Type header", MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
  assertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode());
  verify(response).close();
}

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

@Test  // SPR-16132
public void followUpRequestAfterFailure() {
  MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build();
  server.expect(requestTo("/some-service/some-endpoint"))
      .andRespond(request -> { throw new SocketException("pseudo network error"); });
  server.expect(requestTo("/reporting-service/report-error"))
      .andExpect(method(POST)).andRespond(withSuccess());
  try {
    this.restTemplate.getForEntity("/some-service/some-endpoint", String.class);
  }
  catch (Exception ex) {
    this.restTemplate.postForEntity("/reporting-service/report-error", ex.toString(), String.class);
  }
  server.verify();
}

相关文章