Wednesday, January 28, 2015

Send a HTTP request having a custom Accept header with RestTemplate

To send a HTTP request having a custom Accept header with RestTemplate,

you can do it as follows:

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<Void> requestEntity = new HttpEntity<Void>(headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(
url, HttpMethod.GET, requestEntity, String.class, port, 0, 100);
System.out.println(responseEntity.getBody());

Reference:
http://stackoverflow.com/questions/19238715/how-to-set-an-accept-header-on-spring-resttemplate-request

No comments:

Post a Comment