title: Circular Dependencies in RESTful API with Spring Boot
body: <p>I'm developing a Rest API using Spring Boot, JPA with Hibernate and Maven as repository manager. In my Model I have the class <code>Club</code>:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>@Entity
  @Table( name = "CLUB")
  public class Club {
  
     /**
      * Id da entidade
      */
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     private Long id;
  
      /**
      * Atual técnico do clube.
      */
     @OneToOne(mappedBy = "actualClub")
     private Coach coach;
     
     //Outros atributos, getters e setters
    }</code></pre>
  </div>
  </div>
  </p>
  
  <p>The above class has a <code>@OneToOne</code> relationship with the class <code>Coach</code>:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>@Entity
  @Table(name = "COACH")
  public class Coach extends Person {
  
     /**
      * Clube atual do técnico.
      */
     @OneToOne
     @JoinColumn(name = "CLUB_ID")
     private Club actualClub;
  
     //Outros atributos, getters e setters
     
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>The class <code>Person</code> also has an attribute <strong>id</strong> with <code>@Id</code> and <code>@GeneratedValue</code> annotations.</p>
  
  <p>Lastly, I also have the controller class <code>ClubController</code>, which handle some requests, as you can see below:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>@RestController
  public class ClubController {
  
     /**
      * Instância da classe de serviços da entidade &lt;i&gt;Club&lt;/i&gt;
      */
     @Autowired
     private ClubService clubService;
  
     /**
      * Retorna JSON que representa o clube com o 'id' especificado.
      *
      * @param id Identificador do clube a ser buscado.
      * @return ResponseEntity Objeto com detalhes da requisição HTTP, como o Status.
      */
     @RequestMapping(value = "/clubs/{id}", produces = MediaType.APPLICATION_JSON_VALUE,
             method = RequestMethod.GET)
     public ResponseEntity&lt;?&gt; getClubById(@PathVariable Long id) {
  
        final Club club = this.clubService.findById(id);
        if (club != null) {
           return new ResponseEntity&lt;&gt;(club, HttpStatus.FOUND);
        } else {
           return new ResponseEntity&lt;&gt;("Não encontrado", HttpStatus.NOT_FOUND);
        }
     }
     
     /*Entre outros métodos...*/
     
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>The problem is: in a <code>GET</code> in <code>/clubs/{id}</code> the returned JSON has a circular (infinite) dependency inside it. Club has a Coach which has a Club and and so it goes... X(
  This will also happen with the <code>@OneToMany relationship between</code>Club<code>and</code>Player`...</p>
  
  <p>I've found this solution: <a href="https://dzone.com/articles/circular-dependencies-jackson" rel="nofollow noreferrer">Link</a>. I just have to use the <code>@JsonIdentityInfo</code> annotation in the class declaration. The problem with this solution is the overhead of information that I won't use (Just think in <code>@OneToMany</code> relationship with <code>Player</code>). Besides, as you can see in the above link:</p>
  
  <blockquote>
    <p>"However, JSON id references need to be unique across the entire graph, whereas JPA id’s only need to be unique within the same entity.""</p>
  </blockquote>
  
  <p>So, I'll have to use UUID’s for JPA id fields.</p>
  
  <p>I've also found another possible solution: <a href="https://spring.io/guides/gs/rest-hateoas/" rel="nofollow noreferrer">Spring HATEOAS</a>. In this case, I could add links (href) to some attributes and just would make the requisition <em>"on demand"</em>. But seems that Spring HATEOAS has some limitations, like support for embedding resource...</p>
  
  <p>So, which approach should I follow? Are there other solutions?</p>
  
  <p>Soon enough, I intend to use this API in an iOS app (I don't know if it makes difference).</p>
  
  <p><em>Sorry about my poor English... Greetings from Brazil x)</em></p>