Infinite Recursion with Jackson JSON and Hibernate JPA issue

前端 未结 25 3140
你的背包
你的背包 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:45

    Be sure you use com.fasterxml.jackson everywhere. I spent much time to find it out.

    
      2.9.2
    
    
    
    
      com.fasterxml.jackson.core
        jackson-annotations
        ${fasterxml.jackson.version}
    
    
    
    
      com.fasterxml.jackson.core
        jackson-databind
        ${fasterxml.jackson.version}
    
    

    Then use @JsonManagedReference and @JsonBackReference.

    Finally, you can serialize your model to JSON:

    import com.fasterxml.jackson.databind.ObjectMapper;
    
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(model);
    

提交回复
热议问题