How to serialize lazy loaded entities with jackson module hibernate?
You can try this. It helps to me.
ObjectMapper mapper = new ObjectMapper();Hibernate4Module hbm = new Hibernate4Module();hbm.enable(Hibernate4Module.Feature.FORCE_LAZY_LOADING);mapper.registerModule(hbm);ObjectWriter w = mapper.writer();String result = null;try { result = w.writeValueAsString(o);} catch (JsonProcessingException e) { e.printStackTrace();}
This is how I got it working, i'm using RestEASY on JBoss WildFly
@Provider@Produces(MediaType.APPLICATION_JSON)public class JacksonConfig implements ContextResolver<ObjectMapper> { @Override public ObjectMapper getContext(Class<?> type) { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new Hibernate4Module()); return mapper; }}
I have the exact problem and here is my solution.
public class YourObjectMapper extends ObjectMapper { @PostConstruct public void afterPropertiesSet() throws Exception { getSerializationConfig().set(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); }}
p/s: Only valid for 1.9.x org.codehaus.jackson.map.ObjectMapper
and not 2.x Jackson.