英文:
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 = "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
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论