Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Erro Webservice relacionamento many to many spring boot json

Olá, pessoal!

Estou tendo problemas com um relacionamento many to many no meu webservice do qual não sei resolver. O seguinte cenário abaixo está entrando em loop infinito . Alguém tem alguma sugestão para solução?

Domínio

@Entity
@Table(name = "customers")
public class Customer implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    @Temporal(TemporalType.DATE)
    private Date birthdate;

    private String cpf;

    @ManyToMany
    @JoinTable(name = "customers_contacts", joinColumns = @JoinColumn(name = "customer_id"), inverseJoinColumns = @JoinColumn(name = "customer_id_contact"))
    private List<Customer> contacts = new ArrayList<>();
}

Repositório

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {

    public Page<Customer> findAll(Pageable pageable);
}

Controle

@RestController
@RequestMapping("/rest/clients")
public class ClientResource {

    @Autowired
    CustomerRepository clientRepository;

    @GetMapping
    public Page<Customer> index(CustomerFilter customerFilter, Pageable pageable) {
        return clientRepository.findAll(pageable);
    }
}

Banco De dados

select * from customers;
id |        cpf        |    name
4  | "123.456.789-01"  | "Nicolas"
6  | "987.654.321-02"  | "Fátima"
7  | "012.034.056-03"  | "Rafaela"

select * from customers_contacts;
customer_id | customer_id_contact
    6                7
    4                6
    6                4

Resultado após tentar acessar página

SyntaxError: JSON.parse: unterminated string at line 1 column 119191 of the JSON data
3 respostas

Oi Philip tudo bem?

Não manjo de Java e nem JPA.

Mas parece que o erro está no JSON (tomei o mesmo erro hoje rsrsrs).

Veja se na linha 1 coluna 119191 do JSON que está tentando ser parseado se não há um enter ou um caractere estranho. Enters tem que escapar com \n , tab com \t e outros caracteres via de regra são \ caractere

Espero ter ajudado!!!

Olá, André!

No meu caso é loop infinito mesmo. O JPA está fazendo loop com os registros 6 e 4.

{"content":[{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772.162.952-77","contacts":[]},{"id":4,"name":"Nicolas Thiago da Rocha","birthdate":null,"cpf":"095.044.373-54","contacts":[{"id":6,"name":"Fátima Aparecida Olivia Rocha","birthdate":null,"cpf":"386.079.145-10","contacts":[{"id":7,"name":"Rafaela Lorena das Neves","birthdate":null,"cpf":"772

solução!

Olá, pessoal!

Resolvi utilizando a anotação @JsonIdentityInfo na classe Customer.

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Entity
@Table(name = "customers")
public class Customer implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    @Temporal(TemporalType.DATE)
    private Date birthdate;

    private String cpf;

    @ManyToMany
    @JoinTable(name = "customers_contacts", joinColumns = @JoinColumn(name = "customer_id"), inverseJoinColumns = @JoinColumn(name = "customer_id_contact"))
    private List<Customer> contacts = new ArrayList<>();
}

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software