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

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

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

RestTemplate.<init>介绍

[英]Create a new instance of the RestTemplate using default settings. Default HttpMessageConverter are initialized.
[中]使用默认设置创建RestTemplate的新实例。默认HttpMessageConverter已初始化。

代码示例

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

/**
 * Construct a new instance of the {@link RestGatewaySupport}, with default parameters.
 */
public RestGatewaySupport() {
  this.restTemplate = new RestTemplate();
}

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

/**
 * Creates a new instance of the {@code AsyncRestTemplate} using the given
 * asynchronous and synchronous request factories.
 * @param asyncRequestFactory the asynchronous request factory
 * @param syncRequestFactory the synchronous request factory
 */
public AsyncRestTemplate(org.springframework.http.client.AsyncClientHttpRequestFactory asyncRequestFactory,
    ClientHttpRequestFactory syncRequestFactory) {
  this(asyncRequestFactory, new RestTemplate(syncRequestFactory));
}

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

/**
 * Construct a new instance of the {@link RestGatewaySupport}, with the given {@link ClientHttpRequestFactory}.
 * @see RestTemplate#RestTemplate(ClientHttpRequestFactory)
 */
public RestGatewaySupport(ClientHttpRequestFactory requestFactory) {
  Assert.notNull(requestFactory, "'requestFactory' must not be null");
  this.restTemplate = new RestTemplate(requestFactory);
}

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

@Override
protected RestTemplate initRestTemplate() {
  // JDK default HTTP client blacklist headers like Origin
  return new RestTemplate(new HttpComponentsClientHttpRequestFactory());
}

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

@Before
public void setup() {
  List<HttpMessageConverter<?>> converters = new ArrayList<>();
  converters.add(new StringHttpMessageConverter());
  converters.add(new MappingJackson2HttpMessageConverter());
  this.restTemplate = new RestTemplate();
  this.restTemplate.setMessageConverters(converters);
  this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}

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

@Before
public void setup() {
  List<HttpMessageConverter<?>> converters = new ArrayList<>();
  converters.add(new StringHttpMessageConverter());
  converters.add(new MappingJackson2HttpMessageConverter());
  this.restTemplate = new RestTemplate();
  this.restTemplate.setMessageConverters(converters);
  this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}

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

@Override
protected RestTemplate initRestTemplate() {
  // JDK default HTTP client blacklists headers like Origin
  return new RestTemplate(new HttpComponentsClientHttpRequestFactory());
}

代码示例来源: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 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 // 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 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
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 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 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 testHandlerNotFound() throws Exception {
  URI url = new URI("http://localhost:" + this.port + "/oops");
  RequestEntity<Void> request = RequestEntity.get(url).build();
  try {
    new RestTemplate().exchange(request, byte[].class);
  }
  catch (HttpClientErrorException ex) {
    assertEquals(HttpStatus.NOT_FOUND, ex.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 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);
}

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

@Before
public void setup() {
  this.restTemplate = new RestTemplate();
  this.mockServer = MockRestServiceServer.bindTo(this.restTemplate).ignoreExpectOrder(true).build();
}

相关文章