0

I have developed this webapp using Spring MVC + Hibernate.

I retrieve all my objects in a Service then return them directly to the controller. These objects generally are lazily initialized so collections are empty.

So for object User:

User
{
   int idUser;
   City city;
   String name;
   List<User> friends;
}

I return an object with just idUser and name, City and Friends are not initialized.

I want to take advantage of all my services methods (without modifying them) to provide a REST api, so if from my ApiController I request to get user with id 1, I retrieve all useful information about this user in JSON.

I tried using GSON but as soon as it tries to jsonize the city object it crashes because it has been lazily initialized. Same goes for the friends collection.

For collections it's not much of a big deal since in my api I would have another request url where you can get all friends given a user Id, but in the case of relationships with a single object (like the city in this example), I would like to return the id of the City which by definition of Lazy loading is indeed set.

How can I tell GSON to jsonize just the cityId attribute of City instead of the whole object?

Will nulling the rest of collections be a good solution so they're not converted into JSON? Is there any other way to explicitly tell GSON to ignore these attributes?

Gabriel Sanmartin
  • 3,504
  • 13
  • 47
  • 100
  • If the problem is that GSON cannot handle hibernate proxies you may try to deproxify the object http://stackoverflow.com/questions/2216547/converting-hibernate-proxy-to-real-object . If the problem is GSON triggering lazy loading exception then you need to keep the hibernate session open before calling GSON (e.g. usually in Spring that means you need to still be in transactional block) – Krešimir Nesek Sep 17 '13 at 15:47
  • We ran into this issue with Adobe Flex as well. Our solution back then was to not use Lazy Loading at all, but then you have a performance hit. You may want to try another JSON library, like Jackson, to see if it might handle Hibernate proxies better? Or, does GSON offer any sort of custom serializer? – CodeChimp Sep 17 '13 at 18:25
  • My problem is not with proxies strictly (I'm not getting any of those proxy related errors) but more to the way Gson serializes the associated objects, trying to transform every attribute of them into Json. I would like for specific objects to serialize the ids only, and for the rest of them to simply ignore them (this latter I know how to do) – Gabriel Sanmartin Sep 17 '13 at 18:46
  • I would look at custom serialization then: https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization – CodeChimp Sep 17 '13 at 19:30

1 Answers1

0

I believe you need to put your gson.toJson(...) within a transaction, in springMVC typically @Transactional at controller method where you are doing the actual serialization will do.

If you really want to skip fields or selectively serialize fields of using Gson, you can check https://stackoverflow.com/a/3341439 for gson exclusion strategy. You can skip based on Field Annotation or field name or the entire referenced class.

Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
    public boolean shouldSkipClass(Class<?> clazz) {
        return <class exclusion logic, return true for exclusion else false>;
    }
    public boolean shouldSkipField(FieldAttributes f) {
        return <field exclusion logic>;
    }
 }).create();
Community
  • 1
  • 1
samarjit samanta
  • 1,245
  • 1
  • 16
  • 28