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

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

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

RestTemplate.getForObject介绍

暂无

代码示例

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

private void executeAndVerify() throws URISyntaxException {
  this.restTemplate.getForObject(new URI("/person/1"), Person.class);
  this.mockServer.verify();
}

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

@Test
public void uri() throws InterruptedException, URISyntaxException {
  String result = template.getForObject(baseUrl + "/uri/{query}", String.class, "Z\u00fcrich");
  assertEquals("Invalid request URI", "/uri/Z%C3%BCrich", result);
  result = template.getForObject(baseUrl + "/uri/query={query}", String.class, "foo@bar");
  assertEquals("Invalid request URI", "/uri/query=foo@bar", result);
  result = template.getForObject(baseUrl + "/uri/query={query}", String.class, "T\u014dky\u014d");
  assertEquals("Invalid request URI", "/uri/query=T%C5%8Dky%C5%8D", result);
}

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

@Test
public void getNoContentTypeHeader() throws UnsupportedEncodingException {
  byte[] bytes = template.getForObject(baseUrl + "/get/nocontenttype", byte[].class);
  assertArrayEquals("Invalid content", helloWorld.getBytes("UTF-8"), bytes);
}

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

@Test
public void getNoResponse() {
  String s = template.getForObject(baseUrl + "/get/nothing", String.class);
  assertNull("Invalid content", s);
}

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

@Test
public void getString() {
  String s = template.getForObject(baseUrl + "/{method}", String.class, "get");
  assertEquals("Invalid content", helloWorld, s);
}

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

@Test  // SPR-12123
public void serverPort() {
  String s = template.getForObject("http://localhost:{port}/get", String.class, port);
  assertEquals("Invalid content", helloWorld, s);
}

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

@Test
public void performGetManyTimes() {
  String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
  this.mockServer.expect(manyTimes(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
      .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
  @SuppressWarnings("unused")
  Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
  // We are only validating the request. The response is mocked out.
  // hotel.getId() == 42
  // hotel.getName().equals("Holiday Inn")
  this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
  this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
  this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
  this.mockServer.verify();
}

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

@Test
public void test() throws Exception {
  RestTemplate template = new RestTemplate(new MockMvcClientHttpRequestFactory(this.mockMvc));
  String result = template.getForObject("/foo", String.class);
  assertEquals("bar", result);
}

代码示例来源: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 performGetWithResponseBodyFromFile() {
  Resource responseBody = new ClassPathResource("ludwig.json", this.getClass());
  this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
    .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
  @SuppressWarnings("unused")
  Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
  // hotel.getId() == 42
  // hotel.getName().equals("Holiday Inn")
  this.mockServer.verify();
}

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

@Test // SPR-14694
public void repeatedAccessToResponseViaResource() {
  Resource resource = new ClassPathResource("ludwig.json", this.getClass());
  RestTemplate restTemplate = new RestTemplate();
  restTemplate.setInterceptors(Collections.singletonList(new ContentInterceptor(resource)));
  MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate)
      .ignoreExpectOrder(true)
      .bufferContent()  // enable repeated reads of response body
      .build();
  mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
      .andRespond(withSuccess(resource, MediaType.APPLICATION_JSON));
  restTemplate.getForObject("/composers/{id}", Person.class, 42);
  mockServer.verify();
}

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

@Test
public void ignoreExpectOrder() {
  MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
      .ignoreExpectOrder(true).build();
  server.expect(requestTo("/foo")).andRespond(withSuccess());
  server.expect(requestTo("/bar")).andRespond(withSuccess());
  this.restTemplate.getForObject("/bar", Void.class);
  this.restTemplate.getForObject("/foo", Void.class);
  server.verify();
}

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

@Test
public void resetAndReuseServerWithUnorderedExpectationManager() {
  MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
      .ignoreExpectOrder(true).build();
  server.expect(requestTo("/foo")).andRespond(withSuccess());
  this.restTemplate.getForObject("/foo", Void.class);
  server.verify();
  server.reset();
  server.expect(requestTo("/foo")).andRespond(withSuccess());
  server.expect(requestTo("/bar")).andRespond(withSuccess());
  this.restTemplate.getForObject("/bar", Void.class);
  this.restTemplate.getForObject("/foo", Void.class);
  server.verify();
}

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

@Test
public void resetAndReuseServer() {
  MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build();
  server.expect(requestTo("/foo")).andRespond(withSuccess());
  this.restTemplate.getForObject("/foo", Void.class);
  server.verify();
  server.reset();
  server.expect(requestTo("/bar")).andRespond(withSuccess());
  this.restTemplate.getForObject("/bar", Void.class);
  server.verify();
}

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

@Test(expected = AssertionError.class)
public void expectNeverViolated() {
  String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
  this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
      .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
  this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
      .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
  this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
  this.restTemplate.getForObject("/composers/{id}", Person.class, 43);
}

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

@Test
public void getForObject() throws Exception {
  String expected = "Hello World";
  mockTextPlainHttpMessageConverter();
  HttpHeaders requestHeaders = new HttpHeaders();
  mockSentRequest(GET, "http://example.com", requestHeaders);
  mockResponseStatus(HttpStatus.OK);
  mockTextResponseBody("Hello World");
  String result = template.getForObject("http://example.com", String.class);
  assertEquals("Invalid GET result", expected, result);
  assertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE,
      requestHeaders.getFirst("Accept"));
  verify(response).close();
}

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

@Test
public void performGet() {
  String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
  this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
    .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
  @SuppressWarnings("unused")
  Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
  // We are only validating the request. The response is mocked out.
  // hotel.getId() == 42
  // hotel.getName().equals("Holiday Inn")
  this.mockServer.verify();
}

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

@Test(expected = AssertionError.class)
public void exactExpectOrder() {
  MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
      .ignoreExpectOrder(false).build();
  server.expect(requestTo("/foo")).andRespond(withSuccess());
  server.expect(requestTo("/bar")).andRespond(withSuccess());
  this.restTemplate.getForObject("/bar", Void.class);
}

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

@Test
public void expectNever() {
  String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
  this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
      .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
  this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
      .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
  this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
  this.mockServer.verify();
}

相关文章