Issue
I have written API's which are resulting in 500 error when hit through postman or browser. However, when I debug and see server is not throwing any error and in fact returning a proper response. Other controller I have implemented in a similar way is returning expected result. below is my controller code. Has anyone faced similar situation. Kindly help.
@CrossOrigin
@GetMapping(value="/byPatientId/{patientId}", produces = "application/json")
public List<ContactInfo> getAllContacts(@PathVariable String patientId) {
logger.info("Received request for List of ContactInfo for patientId: "+patientId);
List<ContactInfo> list =
contactInfoService.getAllContacts(patientId);
return list;
}
@CrossOrigin
@GetMapping("/byContactId/{contactId}")
public ContactInfo getContactById(@PathVariable Integer contactId) {
logger.info("Received request for ContactInfo for contactId: "+contactId);
return contactInfoService.getContactById(contactId);
}
Solution
The problem was with one of the dependent object which was having oneToMany relationship with the return type object and it was set to Lazy loading and issue was during the serialization.
Either we can change it to Eager loading or ignore the dependent object by adding
@JsonIgnore
on dependent object.
I handled it by adding @JsonIgnore
annotation on top of the dependent object as I don't need the dependent object in this particular usecase. Issue is solved now.
Answered By - Pradyskumar
Answer Checked By - David Marino (JavaFixing Volunteer)