Search in sources :

Example 66 with RestTemplate

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"));
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) Map(java.util.Map) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 67 with RestTemplate

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);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test)

Example 68 with RestTemplate

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;
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 69 with 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;
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) RestTemplate(org.springframework.web.client.RestTemplate) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) IOException(java.io.IOException) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Bean(org.springframework.context.annotation.Bean)

Example 70 with 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");
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

RestTemplate (org.springframework.web.client.RestTemplate)519 Test (org.junit.Test)135 Test (org.junit.jupiter.api.Test)78 HttpHeaders (org.springframework.http.HttpHeaders)77 HttpEntity (org.springframework.http.HttpEntity)76 URI (java.net.URI)73 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)45 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)44 HashMap (java.util.HashMap)42 ArrayList (java.util.ArrayList)40 IOException (java.io.IOException)36 Bean (org.springframework.context.annotation.Bean)35 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)32 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)29 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)27 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)27 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)24 RestTemplateBuilder (org.springframework.boot.web.client.RestTemplateBuilder)22 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)22 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)22