Example for Spring RestTemplate GET/POST/PUT/DELETE

package rest.api.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
import org.testng.Assert;

public class OAuthRestClient extends SearchServiceUtil {

    private final RestTemplate restTemplate;
    private final static ResourceConfiguration rc = new ResourceConfiguration();
    private final static Logger logger = LoggerFactory.getLogger(OAuthRestClient.class.getName());

    public OAuthRestClient() {
        if(!Constants.secureMode.contains("true")){
            restTemplate = rc.oscsRestTemplateNoAuth();
        }else{
            restTemplate = rc.oscsRestTemplate();
        }
    }

    @Override
    public String executeDelete(String requestUrl, int statusCode, String... strings) {
        logger.info("DELETE " + requestUrl);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // headers.set("x-resource-service-instance-guid", Constants.guid);
        HttpEntity<String> request = new HttpEntity<String>(null, headers);

        String output = "";
        int status = 0;
        try {
            ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.DELETE, request,
                    String.class);
            status = response.getStatusCodeValue();
            output = response.getBody();
        } catch (HttpStatusCodeException e) {
            status = e.getRawStatusCode();
            output = e.getResponseBodyAsString();
        }

        logger.info("======output======" + output);
        Assert.assertEquals(status, statusCode);
        return output;
    }


    @Override
    public String executeGet(String requestUrl, int statusCode, String... strings) {
        logger.info("GET " + requestUrl);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        if(!Constants.secureMode.contains("true")){
             headers.set("x-resource-service-instance-guid", Constants.guid);
             headers.set("idcs_client_id", Constants.idcsClient);
        }

        HttpEntity<String> request = new HttpEntity<String>(null, headers);
        if (strings != null & strings.length > 0) {
            logger.info(strings[0]);
            request = new HttpEntity<String>(strings[0], headers); //Get with request body
        }

        String output = "";
        int status = 0;
        try {
            ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.GET, request, String.class);
            status = response.getStatusCodeValue();
            output = response.getBody();
        } catch (HttpStatusCodeException e) {
            status = e.getRawStatusCode();
            output = e.getResponseBodyAsString();
        }
        logger.info("======output======" + output);
        Assert.assertEquals(status, statusCode);
        return output;
    }


    @Override
    public String executeHead(String requestUrl, int statusCode) {
        logger.info("HEAD " + requestUrl);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // headers.set("x-resource-service-instance-guid", Constants.guid);
        HttpEntity<String> request = new HttpEntity<String>(null, headers);

        String output = "";
        int status = 0;
        try {
            ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.HEAD, request, String.class);
            status = response.getStatusCodeValue();
            output = response.getBody();
        } catch (HttpStatusCodeException e) {
            status = e.getRawStatusCode();
            output = e.getResponseBodyAsString();
        }
        logger.info("======output======" + output);
        Assert.assertEquals(status, statusCode);
        return output;
    }

    @Override
    public String executePost(String requestUrl, String input, int statusCode, String... strings) {
        logger.info("POST " + requestUrl);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // headers.set("x-resource-service-instance-guid", Constants.guid);
        HttpEntity<String> request = new HttpEntity<String>(input, headers);

        String output = "";
        int status = 0;
        try {
            ResponseEntity<String> response = restTemplate.postForEntity(requestUrl, request, String.class);
            status = response.getStatusCodeValue();
            output = response.getBody();
        } catch (HttpStatusCodeException e) {
            status = e.getRawStatusCode();
            output = e.getResponseBodyAsString();
        }
        logger.info("======output======" + output);
        if (statusCode != 0) {
            Assert.assertEquals(status, statusCode);
        }
        return output;
    }

    @Override
    public String executePut(String requestUrl, String input, int statusCode, String... strings) {
        logger.info("PUT " + requestUrl);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // headers.set("x-resource-service-instance-guid", Constants.guid);
        // MediaType type = MediaType.parseMediaType("application/json;
        // charset=UTF-8");
        // headers.setContentType(type);
        // headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<String> request = new HttpEntity<String>(input, headers);

        String output = "";
        int status = 0;
        try {
            ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.PUT, request, String.class);
            status = response.getStatusCodeValue();
            output = response.getBody();
        } catch (HttpStatusCodeException e) {
            status = e.getRawStatusCode();
            output = e.getResponseBodyAsString();
        }
        logger.info("======output======" + output);
        Assert.assertEquals(status, statusCode);
        return output;
    }

}