java.lang.IllegalStateException 无法调用 sendError()

huangapple go评论107阅读模式
英文:

java.lang.IllegalStateException Cannot call sendError()

问题

在使用Postman测试ManyToMany关系的GET请求时,我遇到了以下错误:java.lang.IllegalStateException Cannot call sendError()
现在我看到另一篇帖子建议使用JsonIgnore,虽然它解决了错误,但问题是我也想要JsonIgnore所忽略的部分。

我尝试在我的用户模型类中将JsonIgnore放在@ManyToMany上面,如下所示:

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;

    // 其他getter和setter方法...

}

现在的问题是,当我想要保存、更新或查看(GET)带有爱好的用户时,它会忽略用户模型中的爱好部分,因此用户和爱好之间的关系表保持为空。因为当我在Postman中添加或更新用户时,如下所示:

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

尽管用户已添加或更新,但爱好或user_hobby关系表不会发生任何变化。然而,如果我不使用JsonIgnore,我会得到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 = &quot;users&quot;)
public class User {

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

	@Column(name = &quot;name&quot;)
	private String name;

	@Column(name = &quot;dob&quot;)
	private Date dob;

	@Column(name = &quot;create_date&quot;)
	private LocalDate createDate;

	@Column(name = &quot;update_date&quot;)
	private LocalDate updateDate;
	
	@JsonIgnore
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = &quot;user_hobby&quot;, joinColumns = @JoinColumn(name = &quot;user_id&quot;), inverseJoinColumns = @JoinColumn(name = &quot;hobby_id&quot;))
	private List&lt;Hobby&gt; 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&lt;&gt;();
	}

	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&lt;Hobby&gt; getHobbies() {
		return hobbies;
	}

	public void setHobbies(List&lt;Hobby&gt; 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:

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

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

答案1

得分: 0

创建另一个Java类,并在控制器中将其用作请求体对象。

例如:UserRequest

具有与User相同的字段,在服务类中,从UserRequest准备所需的User实体,并使用它来持久化。

不建议在控制器中使用实体。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月19日 17:19:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定