Durante o curso Java: criando sua primeira API e conectando ao front, ao tentar executar o projeto chamando return repositorio.findAll(); na classe SerieController, eu obtive o seguinte erro:
2025-02-26T15:48:06.240-03:00 ERROR 27056 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError)] with root cause
Classe:
package br.com.alura.screenmatch.controller;
import br.com.alura.screenmatch.model.Serie;
import br.com.alura.screenmatch.repository.SerieRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class SerieController {
@Autowired
private SerieRepository repositorio;
@GetMapping("/series")
public List<Serie> obeterSeries() {
return repositorio.findAll();
}
}