0

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
gene b.
  • 10,512
  • 21
  • 115
  • 227
  • 1
    Can you please provide the exception which is thrown (`500 Internal Server Error`)? – lzagkaretos Dec 18 '17 at 19:24
  • No exceptions are thrown on the server-side, the Controller completes OK. I only see 500 Internal Server Error in Firebug when I get to the AJAX JS on the client-side. – gene b. Dec 18 '17 at 19:30
  • 1
    500 Internal Server Error indicates something is being happening server side. I have seen this kind of issues, in serialization of large objects, `ActivitiesT` is simple or complex object (just guessing) where some kind of Exception was thrown after Controller returns the value. – lzagkaretos Dec 18 '17 at 19:32
  • I got the error msg now, it's this: `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")` – gene b. Dec 18 '17 at 20:12
  • Maybe in the Entity lives some kind of Lazy initialization, and when Jackson tries to access this properties, lists, no transaction is available. Can you check that? – lzagkaretos Dec 18 '17 at 20:14
  • Yeah it looks like there's a thread on it here: https://stackoverflow.com/questions/26957554/jsonmappingexception-could-not-initialize-proxy-no-session The explanation is, "This happens when you are returning an object via @Responsebody (or in your case response body by way of @RestController) and an object is being serialized but has children in a LAZY collection that have not been referenced. By the time you are in your controller there is no longer a transaction active that will facilitate them being fetched" – gene b. Dec 18 '17 at 20:28
  • 1
    Yes, I believe this is the problem. :) Good luck – lzagkaretos Dec 18 '17 at 20:30

1 Answers1

1

When Jackson prepares response, it recursively tries to convert model classes to JSON.For example if you have a Department and Employee relationship with one to many relationship then Jackson executes getEmployees() method in Department.java to prepare the response.But getEmployees() method requires hibernate to execute another query to fetch employee records from DB due to Lazy loading. But the session has been already closed. Hence you get this error. To avoid this, always use DTO instead of actual model classes.And the same thing you have mentioned that - Domain Object Returned from RestController causes AJAX error; Custom Model Object works.

Dhiraj Ray
  • 827
  • 7
  • 11