Java 序列化 Hibernate 对象时抛出奇怪的杰克逊异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4362104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Strange Hymanson exception being thrown when serializing Hibernate object
提问by egervari
Hymanson is throwing a weird exception that I don't know how to fix. I'm using Spring, Hibernate and Hymanson.
Hyman逊抛出了一个我不知道如何解决的奇怪异常。我正在使用 Spring、Hibernate 和 Hymanson。
I have already considered that lazy-loading is causing the problem, but I have taken measures to tell Hymanson to NOT process various properties as follows:
我已经考虑过延迟加载导致了这个问题,但我已经采取措施告诉 Hymanson 不要处理各种属性,如下所示:
@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.
我也对所有其他 UserAccount 子类做了同样的事情。
Here's the exception being thrown:
这是抛出的异常:
org.codehaus.Hymanson.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.Hymanson.map.ser.StdSerializerProvider.serialize(StdSerializerProvider.java:62)
at org.codehaus.Hymanson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:268)
at org.codehaus.Hymanson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:146)
at org.codehaus.Hymanson.map.ser.BeanSerializer.serialize(BeanSerializer.java:118)
at org.codehaus.Hymanson.map.ser.ContainerSerializers$IndexedListSerializer.serializeContents(ContainerSerializers.java:236)
at org.codehaus.Hymanson.map.ser.ContainerSerializers$IndexedListSerializer.serializeContents(ContainerSerializers.java:189)
at org.codehaus.Hymanson.map.ser.ContainerSerializers$AsArraySerializer.serialize(ContainerSerializers.java:111)
at org.codehaus.Hymanson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:296)
at org.codehaus.Hymanson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:224)
at org.codehaus.Hymanson.map.ObjectMapper.writeValue(ObjectMapper.java:925)
at org.springframework.http.converter.json.MappingHymansonHttpMessageConverter.writeInternal(MappingHymansonHttpMessageConverter.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 Hymanson 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.
编辑:我发现代理对象上存在 getHander() 和其他 get*() 方法。GRR!!有什么办法可以告诉Hyman逊不要在代理上处理任何事情,或者我是这样吗?这真的很奇怪,因为吐出 JSON 的方法只会在某些情况下崩溃,而不是一直崩溃。尽管如此,这是由于代理对象上的 get*() 方法。
Aside: Proxies are evil. They disrupt Hymanson, equals() and many other parts of regular Java programming. I am tempted to ditch Hibernate altogether :/
旁白:代理是邪恶的。它们破坏了 Hymanson、equals() 和常规 Java 编程的许多其他部分。我很想完全放弃 Hibernate :/
采纳答案by skaffman
It's not ideal, but you could disable Hymanson'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).
这并不理想,但您可以禁用 Hymanson 对 JSON 属性的自动发现,@JsonAutoDetect
在类级别使用。这将阻止它尝试处理 Javassist 的东西(并失败)。
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.
这意味着您必须手动(使用@JsonProperty
)注释每个 getter ,但这不一定是一件坏事,因为它使事情保持明确。
回答by StaxMan
For what it's worth, there is Hymanson Hibernate moduleproject that just started, and which should solve this problem and hopefully others as well. Project is related to Hymanson project, although not part of core source. This is mostly to allow simpler release process; it will require Hymanson 1.7 as that's when Module API is being introduced.
值得一提的是,Hymanson Hibernate 模块项目刚刚开始,它应该可以解决这个问题,希望其他人也能解决。项目与 Hymanson 项目有关,但不是核心源的一部分。这主要是为了允许更简单的发布过程;它将需要 Hymanson 1.7,因为那时正在引入 Module API。
回答by Claes Mogren
I had a similar problem with lazy loading via the hibernate proxy object. Got around it by annotating the class having lazyloaded private properties with:
我在通过休眠代理对象进行延迟加载时遇到了类似的问题。通过注释具有延迟加载私有属性的类来解决它:
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
I assume you can add the properties on your proxy object that breaks the JSON serialization to that annotation.
我假设您可以在代理对象上添加破坏 JSON 序列化到该注释的属性。
回答by OhadR
i got the same error, but with no relation to Hibernate. I got scared here from all frightening suggestions, which i guess relevant in case of Hibernate and lazy loading... However, in my case i got the error since in an inner class i had no getters/setters, so the BeanSerializer could not serialize the data...
我遇到了同样的错误,但与 Hibernate 无关。我在这里被所有可怕的建议吓到了,我想这些建议与 Hibernate 和延迟加载有关......但是,在我的情况下,我得到了错误,因为在内部类中我没有 getter/setter,所以 BeanSerializer 无法序列化数据...
Adding getters & setters resolved the problem.
添加 getter 和 setter 解决了这个问题。
回答by Jaishankararam
I had the same problem. See if you are using hibernatesession.load()
. If so, try converting to hibernatesession.get()
. This solved my problem.
我有同样的问题。看看你是否正在使用hibernatesession.load()
. 如果是这样,请尝试转换为hibernatesession.get()
. 这解决了我的问题。
回答by Anver Sadhat
I am New to Hymanson API, when i got the "org.codehaus.Hymanson.map.JsonMappingException: No serializer found for class com.company.project.yourclass" , I added the getter and setter to com.company.project.yourclass, that helped me to use the ObjectMapper's mapper object to write the java object into a flat file.
我是 Hymanson API 的新手,当我收到“org.codehaus.Hymanson.map.JsonMappingException: No serializer found for class com.company.project.yourclass”时,我将 getter 和 setter 添加到 com.company.project.yourclass ,这帮助我使用 ObjectMapper 的映射器对象将 java 对象写入平面文件。
回答by atovstonog
You could use @JsonIgnoreProperties(value = { "handler", "hibernateLazyInitializer" })
annotation on your class "Director"
你可以@JsonIgnoreProperties(value = { "handler", "hibernateLazyInitializer" })
在你的班级“导演”上使用注释
回答by Badal
I faced the same issue and It is really strange that the same code works in few case whereas it failed in some random cases.
我遇到了同样的问题,很奇怪,相同的代码在少数情况下有效,而在一些随机情况下却失败了。
I got it fixed by just making sure the proper setter/getter (Making sure the case sensitivity)
我通过确保正确的 setter/getter 来修复它(确保区分大小写)
回答by Howard
You can use Hymanson-datatype-hibernate module to solve this problem. It work for me. reference: https://github.com/FasterXML/Hymanson-datatype-hibernate
您可以使用 Hymanson-datatype-hibernate 模块来解决这个问题。它对我有用。参考:https: //github.com/FasterXML/Hymanson-datatype-hibernate
回答by Remis B
I had the same error message from spring's @RestController
. My rest controller class was using spring's JpaRepository
class and by replacing repository.getOne(id)
method call with repository.findOne(id)
problem was gone.
我从 spring 的@RestController
. 我的休息控制器类正在使用 spring 的JpaRepository
类,并且通过用问题替换repository.getOne(id)
方法调用repository.findOne(id)
消失了。