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

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

问题描述

Jackson 抛出了一个我不知道如何修复的奇怪异常.我正在使用 Spring、Hibernate 和 Jackson.

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

我已经考虑到是延迟加载导致了这个问题,但我已经采取措施告诉 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 {
   ....
}

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

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

这里是抛出的异常:

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?

我发现代理对象上存在 getHander() 和其他 get*() 方法.GRR!!有什么办法可以告诉杰克逊不要在代理上处理任何事情,或者我是这样吗?这真的很奇怪,因为吐出 JSON 的方法只会在某些情况下崩溃,而不是一直崩溃.尽管如此,这是由于代理对象上的 get*() 方法.

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.

旁白:代理是邪恶的.它们破坏了 Jackson、equals() 和常规 Java 编程的许多其他部分.我很想完全放弃 Hibernate :/

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

推荐答案

这并不理想,但您可以在类级别使用 @JsonAutoDetect 禁用 Jackson 对 JSON 属性的自动发现.这将阻止它尝试处理 Javassist 的东西(并失败).

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).

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

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天全站免登陆