Search in sources :

Example 81 with RestTemplate

use of org.springframework.web.client.RestTemplate in project tutorials by eugenp.

the class SpringHttpMessageConvertersLiveTest method givenConsumingXml_whenReadingTheFoo_thenCorrect.

@Test
public void givenConsumingXml_whenReadingTheFoo_thenCorrect() {
    final String URI = BASE_URI + "foos/{id}";
    final RestTemplate restTemplate = new RestTemplate();
    final HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
    final HttpEntity<String> entity = new HttpEntity<String>(headers);
    final ResponseEntity<Foo> response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1");
    final Foo resource = response.getBody();
    assertThat(resource, notNullValue());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Foo(org.baeldung.web.dto.Foo) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test)

Example 82 with RestTemplate

use of org.springframework.web.client.RestTemplate in project tutorials by eugenp.

the class SpringHttpMessageConvertersLiveTest method givenConsumingJson_whenReadingTheFoo_thenCorrect.

@Test
public void givenConsumingJson_whenReadingTheFoo_thenCorrect() {
    final String URI = BASE_URI + "foos/{id}";
    final RestTemplate restTemplate = new RestTemplate();
    final HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    final HttpEntity<String> entity = new HttpEntity<String>(headers);
    final ResponseEntity<Foo> response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1");
    final Foo resource = response.getBody();
    assertThat(resource, notNullValue());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Foo(org.baeldung.web.dto.Foo) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test)

Example 83 with RestTemplate

use of org.springframework.web.client.RestTemplate in project tutorials by eugenp.

the class SpringHttpMessageConvertersLiveTest method whenRetrievingAFoo_thenCorrect.

/**
 * Without specifying Accept Header, uses the default response from the
 * server (in this case json)
 */
@Test
public void whenRetrievingAFoo_thenCorrect() {
    final String URI = BASE_URI + "foos/{id}";
    final RestTemplate restTemplate = new RestTemplate();
    final Foo resource = restTemplate.getForObject(URI, Foo.class, "1");
    assertThat(resource, notNullValue());
}
Also used : Foo(org.baeldung.web.dto.Foo) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test)

Example 84 with RestTemplate

use of org.springframework.web.client.RestTemplate in project tutorials by eugenp.

the class RestClientLiveManualTest method givenAcceptingAllCertificatesUsing4_4_whenHttpsUrlIsConsumedUsingRestTemplate_thenCorrect.

@Test
public final void givenAcceptingAllCertificatesUsing4_4_whenHttpsUrlIsConsumedUsingRestTemplate_thenCorrect() throws ClientProtocolException, IOException {
    final CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
    final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setHttpClient(httpClient);
    final ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);
    assertThat(response.getStatusCode().value(), equalTo(200));
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) RestTemplate(org.springframework.web.client.RestTemplate) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) Test(org.junit.Test)

Example 85 with RestTemplate

use of org.springframework.web.client.RestTemplate in project tutorials by eugenp.

the class LoginController method getLoginInfo.

@GetMapping("/loginSuccess")
public String getLoginInfo(Model model, OAuth2AuthenticationToken authentication) {
    OAuth2AuthorizedClient client = authorizedClientService.loadAuthorizedClient(authentication.getAuthorizedClientRegistrationId(), authentication.getName());
    String userInfoEndpointUri = client.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUri();
    if (!StringUtils.isEmpty(userInfoEndpointUri)) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + client.getAccessToken().getTokenValue());
        HttpEntity<String> entity = new HttpEntity<String>("", headers);
        ResponseEntity<Map> response = restTemplate.exchange(userInfoEndpointUri, HttpMethod.GET, entity, Map.class);
        Map userAttributes = response.getBody();
        model.addAttribute("name", userAttributes.get("name"));
    }
    return "loginSuccess";
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) RestTemplate(org.springframework.web.client.RestTemplate) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) HashMap(java.util.HashMap) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping)

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