Issue
I am getting the below error while reading the data from the rest call Error:-groovy.lang.MissingPropertyException: No such property: response for class: com.school.ClassService. Although my rest call is success because I can see logs in the called API:-
Map<String, Object> httpRequestBody = new HashMap<>();
httpRequestBody.put("name", otherParams.username)
httpRequestBody.put("roll", otherParams.roll)
log.debug(httpRequestBody)
String studentURL = https://dev-student/api/detail/student/marks"
log.debug("URL")
HttpEntity entity = new HttpEntity(httpRequestBody,headers)
log.debug(entity)
try {
ResponseEntity<String> response= restTemplate.exchange(studentURL, HttpMethod.POST, entity, String.class)
}
catch(Exception e){
log.debug(e.printStackTrace())
}
log.info(response)
marksController.java
@PostMapping("/student/marks")
public String generateTotalMarks(@Valid @RequestBody StudentMarksRequest studentMarkRequest) throws Exception {
return caService.getMarks(studentMarkRequest);
Solution
The problem with your code is that response
object is not available outside of the try-catch block.
Any variable which is declared within the try-catch block is only visible within this block. Rewrite your code as follows and it should work:
Map<String, Object> httpRequestBody = new HashMap<>();
httpRequestBody.put("name", otherParams.username)
httpRequestBody.put("roll", otherParams.roll)
log.debug(httpRequestBody)
String studentURL = https://dev-student/api/detail/student/marks"
log.debug("URL")
HttpEntity entity = new HttpEntity(httpRequestBody,headers)
log.debug(entity)
try {
ResponseEntity<String> response= restTemplate.exchange(studentURL, HttpMethod.POST, entity, String.class)
log.info(response)
}
catch(Exception e){
log.debug(e.printStackTrace())
}
Answered By - TheCoolDrop