130

How can I send a GET request using the Spring RestTemplate? Other questions have used POST, but I need to use GET. When I run this, the program continues to work, but it seems that the network is clogged because this is in an AsyncTask, and when I try to run another asynctask after I click on the button for this one, they won't work.

I tried doing

String url = "https://api.blah.com/2.0/search/cubes?w=jdfkl&whitespace=1";

MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("Bearer", accessToken);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //copied this from somewhere else, not sure what its for

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

HttpMessageConverter<String> stringConverter = new StringHttpMessageConverter();
FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
List<HttpMessageConverter<?>> msgConverters = new ArrayList<HttpMessageConverter<?>>();


msgConverters.add(formConverter);
msgConverters.add(new MappingJacksonHttpMessageConverter());
msgConverters.add(stringConverter); 

template.setMessageConverters(msgConverters);
//SetSearchResponseData is my custom class to store the incoming JSON
ResponseEntity<SetSearchResponseData> result = template.exchange(url, HttpMethod.GET, request, SetSearchResponseData.class);
//If I was using post, i could have done SetSearchResponseDataresponse = restTemplate.postForObject(url, request, SetSearchResponseData.class);
Sastrija
  • 3,284
  • 6
  • 47
  • 64
rasen58
  • 4,672
  • 8
  • 39
  • 74

3 Answers3

297

The RestTemplate getForObject() method does not support setting headers. The solution is to use the exchange() method.

So instead of restTemplate.getForObject(url, String.class, param) (which has no headers), use

HttpHeaders headers = new HttpHeaders();
headers.set("Header", "value");
headers.set("Other-Header", "othervalue");
...

HttpEntity<Void> requestEntity = new HttpEntity<>(headers);

ResponseEntity<String> response = restTemplate.exchange(
    url, HttpMethod.GET, requestEntity, String.class, param);

Finally, use response.getBody() to get your result.

This question is similar to this question.

Tristan
  • 8,733
  • 7
  • 48
  • 96
Richard Neish
  • 8,414
  • 4
  • 39
  • 69
  • 1
    or use `getForEntity()`, which offers `getHeaders()` and `getBody()`. – axd Mar 31 '16 at 14:26
  • 33
    @axd getHeaders() will get the _response_ headers, the question is how to set the _request_ headers. – Richard Neish Mar 31 '16 at 18:29
  • 3
    Change from raw type to `HttpEntity> entity = new HttpEntity(headers)` – Ori Marko May 26 '20 at 06:10
  • 1
    Here's a more detailed document from more recent times with example code and all possible variations you may required (as well as deserializing to required response type): https://attacomsian.com/blog/spring-boot-resttemplate-get-request-parameters-headers#get-request-with-parameters-and-headers – Codi Feb 25 '21 at 08:22
  • 1
    @user7294900 : better `HttpEntity requestEntity = new HttpEntity<>(headers);` – Tristan Nov 24 '21 at 11:30
  • Hello @axd how can i add filter to global user? – Rajanboy Jul 19 '22 at 04:12
  • 1
    Hi @Becauseihatemyself that looks like a new or different question. I suggest that you create a new question. – Richard Neish Jul 20 '22 at 08:09
8

Take a look at the JavaDoc for RestTemplate.

There is the corresponding getForObject methods that are the HTTP GET equivalents of postForObject, but they doesn't appear to fulfil your requirements of "GET with headers", as there is no way to specify headers on any of the calls.

Looking at the JavaDoc, no method that is HTTP GET specific allows you to also provide header information. There are alternatives though, one of which you have found and are using. The exchange methods allow you to provide an HttpEntity object representing the details of the request (including headers). The execute methods allow you to specify a RequestCallback from which you can add the headers upon its invocation.

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • 2
    I have looked at it, and `getForObject` doesn't have a parameter that allows me to specify the headers – rasen58 May 28 '13 at 02:30
  • You are correct, it appears that there is not a way to specify the headers. The JavaDoc is there for you though. What is it that doesn't work about your current code? It is hard to tell from your question. – nicholas.hauschild May 28 '13 at 02:59
  • i don't know what's not working because there is no error, but its just not sending or getting the data – rasen58 May 29 '13 at 01:23
1

The getForObject() method of RestTemplate does not support setting headers. you can use this

syntax:

restTemplate.exchange(url endpoint, HttpMethod.GET,entity, params)

public List<Employee> getListofEmployee()
 {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = new HttpEntity<String>(headers);
    ResponseEntity<List<Employee>> response = restTemplate.exchange("http://hello-server/rest/employees",
    HttpMethod.GET,entity, new ParameterizedTypeReference<List<Employee>>() {});
    return response.getBody(); //this returns List of Employee 
  }
anand krish
  • 4,281
  • 4
  • 44
  • 47