Reputation: 1205
This is in reference to a question I asked a month back.
In this question the answer to avoid the lazy init exception when json serializing was to set null to the variables that cause lazy init exception. But consider when the class has many dependencies. Now with the code base is grown and every time I have to set null to the troublesome variables everywhere in the code to avoid json serializing problem. The method does not look neat when the code base is large.
An example code is shown below that doesn't look good.
//setting some variables to avoid lazy init exception in jackson mapper serialization
batch.setEnrollmentList(null);
List<BatchSchedule> scheduleList = (ArrayList<BatchSchedule>) batch.getBatchScheduleList();
for (BatchSchedule batchSchedule : scheduleList) {
batchSchedule.setBatch(null);
}
batch.getLecturer().setBatchList(null);
batch.getLecturer().setSubjectList(null);
batch.getSubject().setBatchList(null);
batch.getSubject().setLecturerList(null);
Can you please suggest me a better way to handle this problem. Thanks.
Upvotes: 4
Views: 1109
Reputation: 10997
You can annotate the lazy properties with @JsonIgnore
so that Jackson ignores it while serializing.
Upvotes: 3