Strange Jackson exception being thrown when serializing Hibernate object

前端 未结 15 2702
离开以前
离开以前 2020-11-29 18:12

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

相关标签:
15条回答
  • 2020-11-29 18:48

    Also you can make your domain object Director final. It is not perfect solution but it prevent creating proxy-subclass of you domain class.

    0 讨论(0)
  • 2020-11-29 18:50

    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.

    0 讨论(0)
  • 2020-11-29 18:51

    I am New to Jackson API, when i got the "org.codehaus.jackson.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.

    0 讨论(0)
  • 2020-11-29 18:53

    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.

    Avoid Jackson serialization on non fetched lazy objects

    0 讨论(0)
  • 2020-11-29 18:54

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

    Adding getters & setters resolved the problem.

    0 讨论(0)
  • 2020-11-29 18:57

    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.

    0 讨论(0)
提交回复
热议问题