Returning POJO with entity objects from custom controller

Hi,

I have been facing this issue for a long time now. While writing custom controller I am having 2 types of response:

  1. List of entities
  2. Custom POJO which itself may contain list of T extends standard entity

In both above cases if I just return as it is from controller I get errors like:

Could not write JSON: Cannot get unfetched attribute [tenant] from detached object com.leapmile.qikpod.entity.Organization-d9ee0db9-1511-19a0-dd23-4489e0401bbd [detached].; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Cannot get unfetched attribute [tenant] from detached object com.leapmile.qikpod.entity.Organization-

where tenant is an attribute which is not part of the view I specified. Now for case 1 above if I use entitySerializationApi.toJson it starts working but for case 2 I am not sure how to proceed.

Not sure if duplicate but I found a related issue https://youtrack.cuba-platform.com/issue/PL-9256 which is marked as closed in 6.6 while I am facing this on 6.9.1

Thanks

Hi,
a custom cuba serialization is only applied when entities are returned either from the entities controller or from the service controller. In case of custom controllers, a standard Jackson serialization is performed and it probably tries to serialize all java class fields. To solve your second case (serializing a POJO) try to use the EntitySerializationAPI#objectToJson method

2 Likes

Thanks, I somehow over looked this method. It works perfectly.