use of org.springframework.web.client.RestTemplate in project spring-boot by Linda-Tan.
the class HelloWorldApplicationTests method simpleClientHttpRequestFactory.
@Test
public void simpleClientHttpRequestFactory() {
// SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
// requestFactory.setConnectTimeout(10000);
// requestFactory.setReadTimeout(10000);
RestTemplate restTemplate = new RestTemplate();
Map map = restTemplate.getForObject("https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=ACCESS_TOKEN", Map.class);
System.out.println(map);
System.out.println(map.get("errcode"));
System.out.println(map.get("errmsg"));
}
use of org.springframework.web.client.RestTemplate in project spring-boot by Linda-Tan.
the class voidTest method getBingPicture.
@Test
public void getBingPicture() throws IOException {
RestTemplate restTemplate = new RestTemplate();
JSONObject jsonObject = restTemplate.getForObject("https://cn.bing.com/HPImageArchive.aspx?format=js&n=1", JSONObject.class);
String sourcePath = jsonObject.getJSONArray("images").getJSONObject(0).getString("url");
String url = "https://cn.bing.com" + sourcePath;
// String usrHome = System.getProperty("user.home");
String savePath = System.getProperty("user.home") + File.separator + "Pictures" + File.separator + "bing";
String filename = sourcePath.substring(sourcePath.lastIndexOf("/"));
// System.out.println(savePath);
// String filename = jsonObject.getJSONArray("images").getJSONObject(0).getString("fullstartdate");
// try {
//
// filename = filename + ".jpg";
// }catch (StringIndexOutOfBoundsException e){
// e.printStackTrace();
// filename = filename.substring(0, filename.indexOf("-")) + ".jpg";
// }
System.out.println(savePath);
IOHelper.downLoadFromUrl(url, filename, savePath);
}
use of org.springframework.web.client.RestTemplate in project spring-boot by Linda-Tan.
the class RestTemplateConfig method restTemplate.
@Bean("restTemplate")
@ConditionalOnMissingBean({ RestOperations.class, RestTemplate.class })
public RestTemplate restTemplate() {
// http://rensanning.iteye.com/?page=3
RestTemplate restTemplate = new RestTemplate(simpleClientHttpRequestFactory());
restTemplate.getMessageConverters().add(new FastJsonHttpMessageConverter());
return restTemplate;
}
use of org.springframework.web.client.RestTemplate in project spring-cloud-sleuth by spring-cloud.
the class SimpleRibbonClientConfiguration method restTemplate.
@Bean
RestTemplate restTemplate() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout(5000);
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
}
});
return restTemplate;
}
use of org.springframework.web.client.RestTemplate in project spring-cloud-sleuth by spring-cloud.
the class TraceFilterWebIntegrationTests method should_create_spans_for_endpoint_returning_unsuccessful_result.
@Test
public void should_create_spans_for_endpoint_returning_unsuccessful_result() {
try {
new RestTemplate().getForObject("http://localhost:" + port() + "/test_bad_request", String.class);
fail("should throw exception");
} catch (HttpClientErrorException e) {
}
then(Tracing.current().tracer().currentSpan()).isNull();
then(this.accumulator.getSpans()).hasSize(1);
then(this.accumulator.getSpans().get(0).kind().ordinal()).isEqualTo(Span.Kind.SERVER.ordinal());
then(this.accumulator.getSpans().get(0).tags()).containsEntry("http.status_code", "400");
then(this.accumulator.getSpans().get(0).tags()).containsEntry("http.path", "/test_bad_request");
}
Aggregations