I am writing client side REST GET call using Spring RestTemplate. I need to pass http headers to the GET call.
Please find the code snippet:
String url = "http://<hostname>:7001/api?id=231";
ResponseEntity<ResponseObject> getEntity = this.restTemplate.getForEntity(url, ResponseObject.class);
return getEntity .getBody();
org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
headers.set("Accept", "application/xml");
headers.set("username", "ABC");
I need to pass above headers like Accept and username in this REST get call. What code changes are needed for the same so that i can pass headers in RestTemplate.
Use your code as:
org.springframework.http.HttpHeaders headers = new
org.springframework.http.HttpHeaders();
headers.set("Accept", "application/xml");
headers.set("username", "ABC");
String url = "http://<hostname>:7001/api?id=231";
ResponseEntity<ResponseObject> getEntity =
this.restTemplate.exchange(url,HttpMethod.GET,new HttpEntity<>( headers),ResponseObject.class);
return getEntity .getBody();