Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KomanRudden/d93718c9d57afc6ba03e5c0ed1954c1e to your computer and use it in GitHub Desktop.
Save KomanRudden/d93718c9d57afc6ba03e5c0ed1954c1e to your computer and use it in GitHub Desktop.
Spring 4 REST Client example - RestTemplate
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.add(ProcessEngineConstants.AUTHOURIZED_USERNAME, username);
HttpEntity<Object> requestEntity = new HttpEntity<Object>("request_headers", headers);
Map<String, Object> restParams = new HashMap<>();
restParams.put("business_key", "123");
try {
ParameterizedTypeReference<anObject> responseType = new ParameterizedTypeReference<anObject>() {};
ResponseEntity<List<NodeDto>> results = restTemplate.exchange("http://localhost:8080/restService/callMe", HttpMethod.GET, requestEntity, responseType, restParams);
if (results.getStatusCode().is4xxClientError() || results.getStatusCode().is5xxServerError()) {
throw new HttpClientErrorException(results.getStatusCode());
}
return results.getBody();
} catch (HttpClientErrorException e) {
throw new ProperCheckedException(e.getMessage(), e.getStatusCode());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment