Strange Jackson exception being thrown when serializing Hibernate object

前端 未结 15 2703
离开以前
离开以前 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:57

    You can add a Jackson mixin on Object.class to always ignore hibernate-related properties. If you are using Spring Boot put this in your Application class:

    @Bean
    public Jackson2ObjectMapperBuilder jacksonBuilder() {
        Jackson2ObjectMapperBuilder b = new Jackson2ObjectMapperBuilder();
        b.mixIn(Object.class, IgnoreHibernatePropertiesInJackson.class);
        return b;
    }
    
    
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    private abstract class IgnoreHibernatePropertiesInJackson{ }
    
    0 讨论(0)
  • 2020-11-29 19:00

    I got the same issue, salutations are here

    Avoid Jackson serialization on non fetched lazy objects

    http://blog.pastelstudios.com/2012/03/12/spring-3-1-hibernate-4-jackson-module-hibernate/

    https://github.com/nessonqk/jackson-datatype-hibernate

    0 讨论(0)
  • 2020-11-29 19:00

    You could use @JsonIgnoreProperties(value = { "handler", "hibernateLazyInitializer" }) annotation on your class "Director"

    0 讨论(0)
  • 2020-11-29 19:00

    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)

    0 讨论(0)
  • 2020-11-29 19:05

    I had the same problem. See if you are using hibernatesession.load(). If so, try converting to hibernatesession.get(). This solved my problem.

    0 讨论(0)
  • 2020-11-29 19:07

    Similar to other answers, the problem for me was declaring a many-to-one column to do lazy fetching. Switching to eager fetching fixed the problem. Before:

    @ManyToOne(targetEntity = StatusCode.class, fetch = FetchType.LAZY)
    

    After:

    @ManyToOne(targetEntity = StatusCode.class, fetch = FetchType.EAGER)
    
    0 讨论(0)
提交回复
热议问题