在序列化Hibernate对象时引发奇怪的杰克逊异常 [英] Strange Jackson exception being thrown when serializing Hibernate object

查看:227
本文介绍了在序列化Hibernate对象时引发奇怪的杰克逊异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

杰克逊抛出了一个奇怪的例外,我不知道该如何解决。我使用Spring,Hibernate和Jackson。



我已经认为懒加载导致了这个问题,但我已经采取措施告诉Jackson不要处理各种属性如下:

pre $ @ $ $ c $ @ @JsonIgnoreProperties({sentMessages,receivedMessages,educationFacility})
public class Director扩展UserAccount implements EducationFacilityUser {
....
}

I对所有其他UserAccount子类也做了同样的事情。



这是抛出的异常:

  org.codehaus.jackson.map.JsonMappingException:找不到类org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer的序列化程序,没有发现创建BeanSerializer的属性(以避免异常,禁用SerializationConfig。 Feature.FAIL_ON_EMPTY_BEANS))(通过引用链:java.util.ArrayList [46]  - > jobprep.domain.educationfacility.Director _ $$ _ javassis t_2 [handler])
at org.codehaus.jackson.map.ser.StdSerializerProvider $ 1.serialize(StdSerializerProvider.java:62)
at org.codehaus.jackson.map.ser.BeanPropertyWriter。 serializeAsField(BeanPropertyWriter.java:268)
位于org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:146)
位于org.codehaus.jackson.map.ser.BeanSerializer。 (org.codehaus.jackson.map.ser.ContainerSerializers
)org.codehaus.jackson.map.ser上的
$ IndexedListSerializer.serializeContents(ContainerSerializers.java:236)
。 ContainerSerializers $ IndexedListSerializer.serializeContents(ContainerSerializers.java:189)
at org.codehaus.jackson.map.ser.ContainerSerializers $ AsArraySerializer.serialize(ContainerSerializers.java:111)
at org.codehaus.jackson。 map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:296)
位于org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSeria lizerProvider.java:224)
at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:925)
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.writeInternal(MappingJacksonHttpMessageConverter。 java:153)

有关如何获取更多信息以查看导致此问题的建议?任何人都知道如何解决它?



编辑:我发现getHander()和其他get *()方法存在于代理对象上。 GRR!有什么方法可以告诉杰克逊不处理代理上的任何事情,或者我是否可以解决?这很奇怪,因为吐出JSON的方法在某些情况下只会崩溃,而不是所有的时间。尽管如此,这是由于代理对象上的get *()方法。



旁白:代理是邪恶的。他们扰乱了Jackson,equals()和其他常规Java编程的其他部分。我试图完全抛弃Hibernate:它不是理想的,但是你可以禁用Jackson的JSON属性自动发现功能,在课堂上使用 @JsonAutoDetect 。这将阻止它试图处理Javassist的东西(和失败)。

这意味着你必须手动注释每个getter(使用 @JsonProperty ),但这不一定是坏事,因为它使事情明确。


Jackson is throwing a weird exception that I don't know how to fix. I'm using Spring, Hibernate and Jackson.

I have already considered that lazy-loading is causing the problem, but I have taken measures to tell Jackson to NOT process various properties as follows:

@JsonIgnoreProperties({ "sentMessages", "receivedMessages", "educationFacility" })
public class Director extends UserAccount implements EducationFacilityUser {
   ....
}

I have done the same thing for all the other UserAccount subclasses as well.

Here's the exception being thrown:

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: java.util.ArrayList[46]->jobprep.domain.educationfacility.Director_$$_javassist_2["handler"])
    at org.codehaus.jackson.map.ser.StdSerializerProvider$1.serialize(StdSerializerProvider.java:62)
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:268)
    at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:146)
    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:118)
    at org.codehaus.jackson.map.ser.ContainerSerializers$IndexedListSerializer.serializeContents(ContainerSerializers.java:236)
    at org.codehaus.jackson.map.ser.ContainerSerializers$IndexedListSerializer.serializeContents(ContainerSerializers.java:189)
    at org.codehaus.jackson.map.ser.ContainerSerializers$AsArraySerializer.serialize(ContainerSerializers.java:111)
    at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:296)
    at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:224)
    at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:925)
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.writeInternal(MappingJacksonHttpMessageConverter.java:153)

Suggestions on how I can get more info to see what's causing this? Anyone know how to fix it?

EDIT: I discovered that getHander() and other get*() methods exist on the proxy object. GRR!! Is there any way I can tell Jackson to not process anything on the proxy, or am I sol? This is really weird because the method that spits out the JSON only crashes under certain circumstances, not all the time. Nonetheless, it's due to the get*() methods on the proxy object.

Aside: Proxies are evil. They disrupt Jackson, equals() and many other parts of regular Java programming. I am tempted to ditch Hibernate altogether :/

解决方案

It's not ideal, but you could disable Jackson's auto-discovery of JSON properties, using @JsonAutoDetect at the class level. This would prevent it from trying to handle the Javassist stuff (and failing).

This means that you then have to annotate each getter manually (with @JsonProperty), but that's not necessarily a bad thing, since it keeps things explicit.

这篇关于在序列化Hibernate对象时引发奇怪的杰克逊异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆