【问题标题】:got error after consuming rest service without @JsonIgnore在没有 @JsonIgnore 的情况下使用休息服务后出现错误
【发布时间】:2019-11-04 10:30:52
【问题描述】:

我有这个映射用户 *------------1 Sexe

用户类

public class User {
    @ManyToOne
    @JoinColumn(name="sexe_id")
    private Sexe sexe;

   // Rest of the Attributes
}

Sexe.class

public class Sexe {    
    @OneToMany(mappedBy="sexe", fetch = FetchType.LAZY)
    private List<User> users;

   // Rest of the Attributes

}

和休息调用的控制器

@GetMapping("/users")
public List<User> getAllUsers()
{
     System.out.println("Get all Users...");
     List<User> users = new ArrayList<>();
     userRepository.findAll().forEach(users::add);
     return users;
}

使用邮递员后,我得到了这个异常:

 at
 com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
 ~[jackson-databind-2.9.6.jar:2.9.6]    at
 com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
 ~[jackson-databind-2.9.6.jar:2.9.6]    at
 com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
 ~[jackson-databind-2.9.6.jar:2.9.6]    at
 com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
 ~[jackson-databind-2.9.6.jar:2.9.6]    at
 com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
 ~[jackson-databind-2.9.6.jar:2.9.6]    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
 ~[jackson-databind-2.9.6.jar:2.9.6]    at
 com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
 ~[jackson-databind-2.9.6.jar:2.9.6]    at
 com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
 ~[jackson-databind-2.9.6.jar:2.9.6]    at
 com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
 ~[jackson-databind-2.9.6.jar:2.9.6]    at
 com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
 ~[jackson-databind-2.9.6.jar:2.9.6]    at
 com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
 ~[jackson-databind-2.9.6.jar:2.9.6]    at

谷歌搜索后,我发现解决方案是使用@JsonIgnore。 这个注释解决了我的问题。但是使用这个注解会导致 Angular 出现问题。

您是否有任何其他建议可以避免这种例外情况?非常感谢您的帮助。

【问题讨论】:

  • 你能添加完整的堆栈跟踪吗,
  • 您好@Avi 先生,感谢您的回复。就是这个异常重复了很多次,stackoverflow不能支持这种重复(警告:最大字符数)

标签: rest spring-boot angular6


【解决方案1】:

JsonIgnore 不是唯一的解决方案。你也可以使用@JsonManagedReference@JsonBackReference

@ManyToOne
@JsonBackReference
@JoinColumn(name="sexe_id")
private Sexe sexe;

@JsonManagedReference
@OneToMany(mappedBy="sexe", fetch = FetchType.LAZY)
private List<User> users;

Could not write JSON: Infinite recursion (StackOverflowError); nested exception spring boot

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多