Search code examples
javaspring-bootspring-data-jpapostman

java.lang.IllegalStateException Cannot call sendError()


while i was using postman to test GET on a ManyToMany relationship I got this error: java.lang.IllegalStateException Cannot call sendError() Now i read another post that says to use JsonIgnore which while it does solve the error the problem is that i also want the the part which JsonIgnore....well ignores.

I tried putting JsonIgnore above @ManyToMany in my user model class like below

package com.user.model;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Table;

@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "name")
    private String name;

    @Column(name = "dob")
    private Date dob;

    @Column(name = "create_date")
    private LocalDate createDate;

    @Column(name = "update_date")
    private LocalDate updateDate;
    
    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "user_hobby", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "hobby_id"))
    private List<Hobby> hobbies;

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public LocalDate getCreateDate() {
        return createDate;
    }

    public void setCreateDate(LocalDate createDate) {
        this.createDate = createDate;
    }

    public LocalDate getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(LocalDate updateDate) {
        this.updateDate = updateDate;
    }

    public User() {
        this.hobbies = new ArrayList<>();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    
    public List<Hobby> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<Hobby> hobbies) {
        this.hobbies = hobbies;
    }
}

now the problem comes when i want to save, update, view(GET) a user with hobbies, it ignores the hobbies part of the user model, so the relationship table between user and hobbies remains empty cause when i add or update a user like this in postman:

{
        "name": "John Doe",
        "dob": "1990-01-01",
        "hobbies": [
            {
                "id": "1"
            },
            {
                "id": "3"
            }
        ]
    }

although the user is added or updated, nothing happens to hobby or user_hobby relationship table, however if I don't use JsonIgnore i get the java.lang.IllegalStateException Cannot call sendError() error


Solution

  • Create another Java class and use that in your controller as Request Body object.

    e.g. UserRequest

    With same fields as User, and In service class prepare desired User Entity from UserRequest and use that to persist.

    It's not recommend to use Entities in Controller.