Created
May 13, 2018 11:50
-
-
Save chathurangat/ba17a9ab36642e6f8aea60a6ca27d403 to your computer and use it in GitHub Desktop.
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
public void findUserById() | |
{ | |
String username = "chathuranga"; | |
String password = "123"; | |
Integer userId = 1; | |
String url = "http://localhost:" + port + "/users/" + userId; | |
//setting up the HTTP Basic Authentication header value | |
String authorizationHeader = "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes()); | |
HttpHeaders requestHeaders = new HttpHeaders(); | |
//set up HTTP Basic Authentication Header | |
requestHeaders.add("Authorization", authorizationHeader); | |
requestHeaders.add("Accept", MediaType.APPLICATION_JSON_VALUE); | |
//request entity is created with request headers | |
HttpEntity<AddUserRequest> requestEntity = new HttpEntity<>(requestHeaders); | |
//adding the query params to the URL | |
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(url) | |
.queryParam("name", "chathuranga") | |
.queryParam("email", "chathuranga.t@gmail.com"); | |
ResponseEntity<FindUserResponse> responseEntity = restTemplate.exchange( | |
uriBuilder.toUriString(), | |
HttpMethod.GET, | |
requestEntity, | |
FindUserResponse.class | |
); | |
if (responseEntity.getStatusCode() == HttpStatus.OK) { | |
System.out.println("response received"); | |
System.out.println(responseEntity.getBody()); | |
} else { | |
System.out.println("error occurred"); | |
System.out.println(responseEntity.getStatusCode()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment