title: How to solve "Could not write JSON: Infinite recursion (StackOverflowError)" in Kotlin using Data Classes?
body: <p>I know there are several posts in the topic, but I just couldn't find one where Kotlin Data Classes were used. So I'm trying to make a REST API with H2 Database in Kotlin, using Spring Boot, and I'm using Postman as well. Some of the attributes of my classes have List type. And everytime I try to add some value to these lists in Postman and then try to get the results, I get the following error:</p>
  <p><a href="https://i.stack.imgur.com/Rz29j.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>I have three classes:</p>
  <p>Recipe.kt:</p>
  <pre><code>@Entity
  data class Recipe(
      @Id
      @SequenceGenerator(name = RECIPE_SEQUENCE, sequenceName = RECIPE_SEQUENCE, initialValue = 1, allocationSize = 1)
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      var id: Long = 0,
      val name: String,
      var cookTime: String?,
      var servings: String?,
      var directions: String?,
      @OneToMany(cascade = [CascadeType.ALL], mappedBy = &quot;recipe&quot;)
      @JsonManagedReference
      var ingredient: List&lt;Ingredient&gt;?,
      @ManyToMany
      @JsonManagedReference
      @JoinTable(
          name = &quot;recipe_category&quot;,
          joinColumns = [JoinColumn(name = &quot;recipe_id&quot;)],
          inverseJoinColumns = [JoinColumn(name = &quot;category_id&quot;)]
      )
      var category: List&lt;Category&gt;?,
      @Enumerated(value = EnumType.STRING)
      var difficulty: Difficulty?
      ) { companion object { const val RECIPE_SEQUENCE: String = &quot;RECIPE_SEQUENCE&quot; } }
  </code></pre>
  <p>Category.kt</p>
  <pre><code>    @Entity
  data class Category(
      @Id
      @SequenceGenerator(name = CATEGORY_SEQUENCE, sequenceName = CATEGORY_SEQUENCE, initialValue = 1, allocationSize = 1)
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      val id: Long = 0,
      var name: String,
      @ManyToMany(mappedBy = &quot;category&quot;)
      @JsonBackReference
      var recipe: List&lt;Recipe&gt;?
  ) { companion object { const val CATEGORY_SEQUENCE: String = &quot;CATEGORY_SEQUENCE&quot; } }
  </code></pre>
  <p>Ingredient.kt</p>
  <pre><code>    @Entity
  data class Ingredient(
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      val id: Long = 0,
      var description: String?,
      var amount: BigDecimal?,
      @ManyToOne
      @JsonBackReference
      var recipe: Recipe?,
      var unitOfMeasure: String?
  )
  </code></pre>
  <p>RecipeResponse.kt</p>
  <pre><code>data class RecipeResponse (var id:Long,
                             var name:String,
                             var cookTime:String?,
                             var servings:String?,
                             var directions:String?,
                             var ingredient:List&lt;Ingredient&gt;?,
                             var category: List&lt;Category&gt;?,
                             var difficulty: Difficulty?)
  </code></pre>
  <p>RecipeResource.kt</p>
  <pre><code>@RestController
  @RequestMapping(value = [BASE_RECIPE_URL])
  class RecipeResource(private val recipeManagementService: RecipeManagementService)
  {
  
      @GetMapping
      fun findAll(): ResponseEntity&lt;List&lt;RecipeResponse&gt;&gt; = ResponseEntity.ok(this.recipeManagementService.findAll())
  </code></pre>
  <p>RecipeManagementService.kt</p>
  <pre><code>@Service
  class RecipeManagementService (@Autowired private val recipeRepository: RecipeRepository,
                                 private val addRecipeRequestTransformer: AddRecipeRequestTransformer) {
  
      fun findAll(): List&lt;RecipeResponse&gt; = this.recipeRepository.findAll().map(Recipe::toRecipeResponse)
  </code></pre>