invictvs1
invictvs1

Reputation: 479

JsonMappingException while saving Java Object to JSON

 org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) through reference chain..

Above is the exception I'm getting, where my non-primitive member variables in my pojo are not able to be read. after several searches I tried add @JsonAutoDetect for my POJO class and also tried the @JsonProperty annotation for all fields that weren't getting recognized. This did not work so I also tried @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) with no luck as well.

Here is where I attempt to convert the object into json:

@Override
public Map<String, String> getChanges(User oldInfo, User newInfo) {
    oldAndNewInfo = new HashMap<String, String>();
    objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter();
    try {
        oldAndNewInfo.put(objectWriter.writeValueAsString(oldInfo), objectWriter.writeValueAsString(newInfo));
    } catch (Exception e) {
        e.printStackTrace(ps);
    }
    return oldAndNewInfo;
}

Does anyone have any ideas?

Upvotes: 1

Views: 1246

Answers (1)

AnthonyJClink
AnthonyJClink

Reputation: 1008

I think you should disable the SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS property.

A good way to keep your object mappers inline (and as a best pratice) its best to save them in some kind of module.

Guarded singleton, Guice config, Spring config, some kinda storage places for singletons. ill use a guarded singleton in my example, but this is pretty much what the docs are sayin.

Also remember that object mappers, for xml or json are very expensive to build. if you do alot of serialization, save it

public abstract class JsonConfig{

    private JsonConfig(){

    }

    private static final ObjectMapper CONFIGURED_OBJECT_MAPPER;
    private static final JacksonJsonProvider JAX_RS_JSON_PROVIDER;

    static{
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
        objectMapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
        objectMapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true);

        //and just incase you do some web transformaions with jersy or something

        JAX_RS_JSON_PROVIDER = new JacksonJsonProvider(objectMapper);

        CONFIGURED_OBJECT_MAPPER = objectMapper;    
    }

    public static ObjectMapper getObjectMapperInstance(){
        return CONFIGURED_OBJECT_MAPPER;
    }

    public static JacksonJsonProvider getJsonProviderInstance(){
       return return JAX_RS_JSON_PROVIDER
    }
}

Upvotes: 2

Related Questions