zoukankan      html  css  js  c++  java
  • JSON: Circular Dependency Errors

    If you’re using Object Relational Mapping frameworks like Hibernate, and are using the bi-directional mappings then you can be sure of getting the following errors if you try to generate JSON of the entities involved.

    1. If you’re using Jackson

    1
    com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)

    2. If you’re using Google’s Gson

    1
    java.lang.IllegalStateException: circular reference error

    Let’s take a case of two entities Actor, and Movie having a bi-directional relationship and using them we’ll see how to get over the above exceptions when we try to generate JSON.

    This solution uses Jackson for generating JSON.

    So if you’re using Google’s Gson you’ll have to write an exclusion strategy yourself, and more importantly the rest of the post is useless for you, unless you’re willing to switch to Jackson like I did with such great finesse! ;)

    If yes, then you can download Jackson’s libraries and continue reading the post further, rest assured that it’ll be of immense help to you! :)

    Now, these entities will have a many-to-many relationship amongst them. As one Actor can act in many Movies and one Movie can have many Actors (as it so often happens, and by the way due apologies for being trite and rhetoric!).

    Actor.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @Entity
    @Table(name="Actor")
    public class Actor implements Serializable {
      . . .
           /* This is the exception-saving-annotation */
           @JsonManagedReference
           @ManyToMany
           private List<Movie> movies;
      . . .
    }

    Movie.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @Entity
    @Table(name="Movie")
    public class Movie implements Serializable {
     . . .
          /* This is the exception-saving-annotation */
          @JsonBackReference
          @ManyToMany(mappedBy="movies")
          private List<Actor> actors;
     . . .
    }

    So all we did was adding two annotations to the fields having a relationship.

    1. @JsonManagedReference: This annotation should be put on the owner of the relationship, or as you may deem fit.
    2. @JsonBackReference: This annotation should be used on the other end of the relationship.

    Now, having done that it’s extremely easy to generate the JSON.

    1
    2
    3
    4
    5
    6
    //com.fasterxml.jackson.databind.ObjectMapper
    ObjectMapper objectMapper = new ObjectMapper();
    //The input argument of the writeValueAsString() function can be a bean, array, list, map or a set.
    String actorsAsJson = objectMapper.writeValueAsString(actorsList);
    //actorsList is a variable of type List<Actor>
    //Voila! You're done!

    That’s all for the circular dependency errors.

    source :https://ankeetmaini.wordpress.com/2012/07/26/json-troubleshooting-circular-dependency-errors/

  • 相关阅读:
    linuxepoll研究 Geek_Ma 博客园
    socklen_t 类型 blueliuyun的专栏 博客频道 CSDN.NET
    自己动手写web服务器一(浏览器的访问信息) 任天胜的个人空间 开源中国社区
    UNIX Domain Socket IPC blueliuyun的专栏 博客频道 CSDN.NET
    How to use epoll? A complete example in C
    gzip头部格式 任天胜的个人空间 开源中国社区
    CWnd与HWND的区别与转换
    MFC 框架各部分指针获取方式
    windows 注册表的编程
    VS2010创建C++项目类向导和智能感知不可用
  • 原文地址:https://www.cnblogs.com/sdream/p/5207221.html
Copyright © 2011-2022 走看看