Infinite Recursion with Jackson JSON and Hibernate JPA issue

前端 未结 25 3592
你的背包
你的背包 2020-11-21 07:31

When trying to convert a JPA object that has a bi-directional association into JSON, I keep getting

org.codehaus.jackson.map.JsonMappingException: Infinite          


        
25条回答
  •  后悔当初
    2020-11-21 07:39

    There's now a Jackson module (for Jackson 2) specifically designed to handle Hibernate lazy initialization problems when serializing.

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

    Just add the dependency (note there are different dependencies for Hibernate 3 and Hibernate 4):

    
      com.fasterxml.jackson.datatype
      jackson-datatype-hibernate4
      2.4.0
    
    

    and then register the module when intializing Jackson's ObjectMapper:

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new Hibernate4Module());
    

    Documentation currently isn't great. See the Hibernate4Module code for available options.

提交回复
热议问题