Created
May 5, 2016 06:39
-
-
Save KomanRudden/d93718c9d57afc6ba03e5c0ed1954c1e to your computer and use it in GitHub Desktop.
Spring 4 REST Client example - RestTemplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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