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..
上面是我得到的例外情况,我的pojo中的非原始成员变量无法被读取。在多次搜索之后,我尝试为我的POJO类添加@JsonAutoDetect
,并为所有未被识别的字段尝试了@JsonProperty
注释。这没用,所以我也尝试了@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
而没有运气。
这是我尝试将对象转换为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;
}
有没有人有任何想法?
答案 0 :(得分:2)
我认为你应该禁用SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS属性。
保持对象映射器内联(以及最佳实践)的最佳方法是将它们保存在某种模块中。
守卫单身人士,Guice配置,Spring配置,单身人士的一些存储空间。 在我的例子中使用了一个受保护的单身人士,但这几乎是文档所说的。
还要记住,xml或json的对象映射器构建起来非常昂贵。如果你做了很多序列化,保存它
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
}
}