RestTemplate restTemplate = new RestTemplate();
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
ResponseEntity<Foo> response = restTemplate
  .exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class);
  
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
  
Foo foo = response.getBody();
  
assertThat(foo, notNullValue());
assertThat(foo.getName(), is("bar"));

        

private static void getEmployees()
{
    final String uri = "http://localhost:8080/springrestexample/employees";
     
    RestTemplate restTemplate = new RestTemplate();
     
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
     
    ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
     
    System.out.println(result);
}

        

public class ExchangeDemo {
    public static void main(String args[]) {
        RestTemplate restTemplate = new RestTemplate();
	String uri = "http://localhost:8080/spring-rest/data/exchange/{id}";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<String>("Hello World!", headers);
        ResponseEntity<Person> personEntity = restTemplate.exchange(uri, HttpMethod.GET, entity, Person.class, 100);
        System.out.println("ID:"+personEntity.getBody().getId());
        System.out.println("Name:"+personEntity.getBody().getName());
        System.out.println("Village:"+personEntity.getBody().getAddress().getVillage());
    }
}

        

@RestController
public class ConsumeWebService {
   @Autowired
   RestTemplate restTemplate;

   @RequestMapping(value = "/template/products")
   public String getProductList() {
      HttpHeaders headers = new HttpHeaders();
      headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
      HttpEntity <String> entity = new HttpEntity<String>(headers);
      
      return restTemplate.exchange("
         http://localhost:8080/products", HttpMethod.GET, entity, String.class).getBody();
   }
}

        

@RestController
@RequestMapping("/todos")
public class TodoController {
    @Autowired
    RestTemplate restTemplate;
    @GetMapping
    public List<TodoModel> getTodos() {
        String theUrl = "https://jsonplaceholder.typicode.com/todos";
        ResponseEntity<List<TodoModel>> response = restTemplate.exchange(theUrl, HttpMethod.GET, null, new ParameterizedTypeReference<List<TodoModel>>() {
        });
        List<TodoModel> todoList = response.getBody();
        return todoList;
    }
}

        

public Map<String, String> getUserInfoFor(OAuth2AccessToken accessToken) {
    RestTemplate restTemplate = new RestTemplate();

    RequestEntity<MultiValueMap<String, String>> requestEntity = new RequestEntity<>(
            getHeader(accessToken),
            HttpMethod.GET,
            URI.create("https://www.googleapis.com/oauth2/v3/userinfo")
    );

    ResponseEntity<Map> result = restTemplate.exchange(
            requestEntity, Map.class);

    if (result.getStatusCode().is2xxSuccessful()) {
        return result.getBody();
    }

    throw new RuntimeException("It wasn't possible to retrieve userInfo");
}

        

public String getMarathonServiceDetails() {
    try {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity =
                restTemplate.exchange(props.getMarathon().getUrl() + "/v2/apps", HttpMethod.GET, null, String.class);

        if (responseEntity.getStatusCode().value() != 200) {
            LOG.error("error marathon service failed with status code " + responseEntity.getStatusCode().value());
            return null;
        }
        isHealthy = true;
        if (LOG.isTraceEnabled()) {
            LOG.trace("marathon services details: " + responseEntity.getBody());
        }
        return responseEntity.getBody();
    } catch (RestClientException e) {
        LOG.error("error in calling marathon service details: ", e);
        isHealthy = false;
        return null;
    }
}

        

public static CompositeAccessToken getAccessAndRefreshToken(String oauthEndpoint, String code, DashboardClient dashboardClient,
                                                      String redirectUri) throws RestClientException {
    String clientBasicAuth = getClientBasicAuthHeader(dashboardClient.getId(),  dashboardClient.getSecret());
    RestTemplate template = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.AUTHORIZATION, clientBasicAuth);
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    MultiValueMap<String,String> form = new LinkedMultiValueMap<>();
    form.add("response_type", "token");
    form.add("grant_type", "authorization_code");
    form.add("client_id", dashboardClient.getId());
    form.add("client_secret", dashboardClient.getSecret());
    form.add("redirect_uri", redirectUri);
    form.add("code", code);

    ResponseEntity<CompositeAccessToken> token = template.exchange(oauthEndpoint + "/token",
            HttpMethod.POST, new HttpEntity<>(form, headers), CompositeAccessToken.class);

    if (token != null)
        return token.getBody();
    else
        return null;
}

        

public void testLogin() {
	try {
		String authUrl = String.format(AUTH_URL_BASE, port);
		JsonObject request = Json.createObjectBuilder()
				.add("id", DEFAULT_ID)
				.add("key", DEFAULT_KEY)
				.build();

		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		HttpEntity<String> entity = new HttpEntity<>(request.toString(), headers);
		RestTemplate rest = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
		ResponseEntity<String> response = rest.exchange(authUrl, HttpMethod.POST, entity, String.class);

		assertEquals(200, response.getStatusCodeValue());
		JsonReader reader = Json.createReader(new StringReader(response.getBody()));
		JsonObject o = reader.readObject();
		String token = o.getString("token");
		assertTrue(!StringUtils.isEmpty(token));
		logger.info(token);
	} catch (Exception e) {
		logger.error(e.getMessage(), e);
		fail(e.getMessage());
	}
}

        

public GitHubEmail getPrimaryEmail(final String gitHubToken) {
    RestTemplate template = new GitHubRestTemplate(gitHubToken);
    ResponseEntity<List<GitHubEmail>> response = template.exchange(GITHUB_EMAIL_URL, HttpMethod.GET, null, new ParameterizedTypeReference<List<GitHubEmail>>(){});
    List<GitHubEmail> emails = response.getBody();
    GitHubEmail primary = emails.stream().filter(e -> e.isPrimary()).findFirst().get();
    return primary;
}        
main