Skip to content

Instantly share code, notes, and snippets.

@danielbryantuk
Last active August 29, 2015 14:01
Show Gist options
  • Save danielbryantuk/9446b5887c170e98b259 to your computer and use it in GitHub Desktop.
Save danielbryantuk/9446b5887c170e98b259 to your computer and use it in GitHub Desktop.
@Component
public class CorrelatingRestClient implements RestClient {
private RestTemplate restTemplate = new RestTemplate();
@Override
public String getForString(String uri) {
String correlationId = RequestCorrelation.getId();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(RequestCorrelation.CORRELATION_ID, correlationId);
LOGGER.info("start REST request to {} with correlationId {}", uri, correlationId);
//TODO: error-handling and fault-tolerance in production
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET,
new HttpEntity<String>(httpHeaders), String.class);
LOGGER.info("completed REST request to {} with correlationId {}", uri, correlationId);
return response.getBody();
}
}
//... calling Class
public String exampleMethod() {
RestClient restClient = new CorrelatingRestClient();
return restClient.getForString(URI_LOCATION); //correlation id handling completely abstracted to RestClient impl
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment