Vin*_*ini 4 java spring jackson
尝试访问端点http://localhost:8080/categoryOfPermissions时收到错误“无法写入 JSON:无限递归”。我在这里研究并找到了各种解决方案(、、),但似乎都不起作用。最后,我找到了一个答案,指出有必要从“设置”更改为“列表”,才能使解决方案发挥作用。我测试了它,从“设置”更改为“列表”后,它确实开始工作。@JsonManagedReference / @JsonBackReference@JsonIgnore@JsonIdentityInfo@JsonIdentityInfo
我觉得这很奇怪,但我发现了更奇怪的事情:从 Set 更改为 List 后,我删除了注释@JsonIdentityInfo,一切都继续工作。换句话说,我真正需要做的就是从 Set 更改为 List 以消除异常。没有其他的。不需要任何解决方案:@JsonManagedReference / @JsonBackReference,,@JsonIgnore。@JsonIdentityInfo
下面是产生异常的代码。我所要做的就是更改private Set<Permission> permission为private List<Permission> permission.
我想知道为什么,特别是因为我更喜欢使用 Set,以避免 Hibernate 使用“Bags”范例(这可能会导致一些不良行为)。
权限.java:
@Entity
@Data
public class Permission{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotBlank
private String name;
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_of_permission_id")
private CategoryOfPermission categoryOfPermission;
}
Run Code Online (Sandbox Code Playgroud)
CategoryOfPermission.java :
@Entity
@Data
public class CategoryOfPermission{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotBlank
private String name;
@NotNull
@OneToMany(mappedBy = "categoryOfPermission", fetch=FetchType.LAZY)
private Set<Permission> permission;
}
Run Code Online (Sandbox Code Playgroud)
CategoryOfPermissionRepo.java :
public interface CategoryOfPermissionRepo extends CrudRepository<CategoryOfPermission, Integer>{
}
Run Code Online (Sandbox Code Playgroud)
hat*_*oor 11
这是因为SetinJava使用equals契约来判断两个对象是否相同,而类equals中方法Permission的实现方式(using lombok)导致了无限递归。
equals它是方法 生成代码的一部分Permission
Object this$categoryOfPermission = this.getCategoryOfPermission();
Object other$categoryOfPermission = other.getCategoryOfPermission();
if (this$categoryOfPermission == null) {
if (other$categoryOfPermission != null) {
return false;
}
} else if (!this$categoryOfPermission.equals(other$categoryOfPermission)) {
return false;
}
Run Code Online (Sandbox Code Playgroud)
它是该类的生成CategoryOfPermission代码
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof CategoryOfPermission)) {
return false;
} else {
CategoryOfPermission other = (CategoryOfPermission)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id != null) {
return false;
}
} else if (!this$id.equals(other$id)) {
return false;
}
Object this$permission = this.getPermission();
Object other$permission = other.getPermission();
if (this$permission == null) {
if (other$permission != null) {
return false;
}
} else if (!this$permission.equals(other$permission)) {
return false;
}
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的Permission,类调用类equals的方法CategoryOfPermission并CategoryOfPermission调用类equals的方法Permision,最终导致堆栈溢出问题!
| 归档时间: |
|
| 查看次数: |
896 次 |
| 最近记录: |