I have an issue in my SpringMVC RestController
that writes JSON results:
1) When I return a domain object as below, ActivitiesT, I get an AJAX 500 Internal Server Error on the client-side.
@RequestMapping("/participant/activityForEvent")
public ActivitiesT getActivityForGuiEventId() throws Exception {
ActivitiesT activitiesT = participantService.getActivity();
return activitiesT;
}
ActivitiesT Domain object, Hiberate-generated:
@Entity
@Table(name = "activities_t", schema = "public")
public class ActivitiesT implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
//... etc...
}
2) But when I return a custom POJO, the AJAX method works. It's almost the same.
@RequestMapping("/participant/activityForEvent")
public ActivitiesT getActivityForGuiEventId() throws Exception {
ActivitiesT activitiesT = participantService.getActivity();
// Create a custom Activity POJO and return it
return new Activity(activitiesT.id, activitiesT.title);
}
Activity custom POJO:
public class Activity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public int id;
public String title;
//etc.
ERROR MESSAGE
HTTP Status 500 - Could not write JSON: could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy -
no Session (through reference chain: com.myapp")
Some things to note:
- Both objects implement
Serializable
, that's fine - The issue is not DAO, I can see the DAO always returns correct data for both, we can ignore the DAO piece
- The Controller is annotated with
@RestController
, so it always outputs JSON; I don't need a ResponseBody or the older tags
Any thoughts on where the issue may lie? The problem boils down to this:
- I can't return JSON from RestController methods on Hibernate-generated @Entity POJOs,
- But I can return JSON from RestController methods on my own custom POJOs