I am trying to call get request using RestTemplate
. I tried like below But I am getting error HttpClientErrorException$BadRequest: 400 null
.
Can any one tell me what I am doing wrong in calling Get Request with parameters?
@GetMapping("/getDepartmentInstitute")
public Integer getDeptInstByUserIDAndInstId(@RequestParam Integer nUserId,
@RequestParam Integer nDeptInst) {
return permissionsService.findDeptInstByUserIdAndInstId(nUserId,nDeptInst );
}
I tried like this
public Integer findAlertDetails(Integer nUserId, Integer nInstId) {
String uri="http://localhost:8064/spacestudy/" +instituteIdentifier +"/communication/alertmanagement/getDepartmentInstitute?nUserId="+ nUserId+"&nInstId="+nInstId+"";
RestTemplate restTemplate = new RestTemplate();
Integer nInstTo=restTemplate.getForObject(uri,Integer.class,nUserId,nInstId);
System.out.println(nInstTo);
return nInstTo;
}
restTemplate.getForObject(String url, Class responseType, Object... uriVariables) is supposed to deal with uri variables like {myVar} in your String url.
If you want to set query parameters, consider using the UriComponentsBuilder.
URI uri = UriComponentsBuilder.fromHttpUrl("http://localhost:8064/spacestudy/" +instituteIdentifier +"/communication/alertmanagement/getDepartmentInstitute")
.queryParam("nUserId", nUserId)
.queryParam("nDeptInst", nInstId)
.build().toUri();
Integer nInstTo = restTemplate.getForObject(uri,Integer.class);
Or you can set them manually like :
String uri = "http://localhost:8064/spacestudy/" +instituteIdentifier +"/communication/alertmanagement/getDepartmentInstitute"
+ "?nUserId=" + nUserId + "&nDeptInst=" + nInstId;
I prefer the UriComponentsBuilder solution.