micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.07k stars 1.07k forks source link

How to perform serialization using ObjectMapper() #1397

Closed ObaidShahid closed 5 years ago

ObaidShahid commented 5 years ago

I am facing issues while returning response from "/index" method. As far as in grails i have handled this error using marshaller but marshaller cannot be used in micronaut : See Error logs

{
    "message": "Internal Server Error: Error encoding object [{dashboards=[com.ef.Domains.Dashboard : 1, com.ef.Domains.Dashboard : 2, com.ef.Domains.Dashboard : 3], status=OK}] to JSON: failed to lazily initialize a collection of role: com.ef.Domains.Dashboard.dashboardGadgets, could not initialize proxy - no Session (through reference chain: java.util.LinkedHashMap[\"dashboards\"]->java.util.ArrayList[0]->com.ef.Domains.Dashboard[\"dashboardGadgets\"])"
}

I have tried the followings in my code here:

 @Get("/index")
    @Transactional
    @JsonIgnore
    def index(HttpParameters P){
        def resultSet = [:]
        try {
             def dashboardsList = Dashboard.list()

             ObjectMapper mapper = new ObjectMapper();
//            Serialization from Object to JSON          
              dashboardsList  =   
              mapper.writerWithDefaultPrettyPrinter().writeValueAsString(dashboardsList);

            resultSet.put("dashboards", dashboardsList);
            resultSet.put("status", HttpStatus.OK);

        } catch (Exception ex) {

            resultSet.put("status", HttpStatus.INTERNAL_SERVER_ERROR);

            log.info("Some Error Occurred while Fetching List of Dashboards. See Error Logs for More Details.");
            log.error("Occurred while Fetching List of Dashboards", ex);
        }
        return resultSet
    }

Any help will be appreciated

graemerocher commented 5 years ago

Please ask questions on gitter https://gitter.im/micronautfw