简体   繁体   English

com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.Z93F725A07423FE1C889F448B33BootD21F:248B33BootD21F

[英]com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25 SpringBoot

I have a couple one to many relationships in spring between my classes.我的班级之间在 spring 中有一对多的关系。 And when i try to get all my data an error like infinite recursion, here is the whole error message.当我试图让我的所有数据像无限递归这样的错误时,这里是整个错误消息。

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]

i have 2 classes that are connected with each other with OneToMany Relationship我有 2 个类通过 OneToMany 关系相互连接

USER CLASS用户 CLASS

public class User  {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;
    @OneToMany(mappedBy = "User")
    private List<Conversation> Conversation;

CONVERSATION CLASS对话 CLASS

public class Conversation {
     @Id  @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;
     @ManyToOne
     @JoinColumn(name = "User_id")
     private User User;

Results screeshot结果截图

Results screeshot结果截图

I don't know why it happens, but maybe it is because of the relationship with the class Failure.我不知道为什么会发生,但可能是因为与 class 故障的关系。

Repository Class存储库 Class

@Query(nativeQuery = true, value = "SELECT * FROM `conversation` WHERE 1")
public List<Conversation> findAllConv();

Controller class Controller class

 @Autowired
        private ConversationRepository conv;
        
        @GetMapping("/GetAllConversation")
        public List<Conversation> getAllConv()
        {
            return conv.findAllConv();
        }
    

Full Trace全跟踪

2021-05-14 12:58:57.306 ERROR 1540 --- [nio-8020-exec-1] w.s.e.ErrorMvcAutoConfiguration$SpelView : Cannot render error page for request [/survey/GetAllConversation/] and exception [Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.club.entities.User_$$_jvstf65_b["conversation"]->org.hibernate.collection.internal.PersistentBag[0]->org.club.entities.Conversation["user"]-

When using relations in general, you can adjust the "fetch" and "cascade" type.一般使用关系时,可以调整“fetch”和“cascade”类型。 Fetching means, if all entries related to the one you want to load are loaded as well while cascading relates to operations on the current entry being passed down to relating objects.获取意味着,如果与您要加载的条目相关的所有条目也已加载,而级联与当前条目上的操作相关,则将其传递给相关对象。

Edit1: I would suggest you try setting the fetch type to lazy at least for the Conversation class: Edit1:我建议您尝试将fetch类型设置为惰性,至少对于Conversation class:

@ManyToOne(fetch = FetchType.LAZY)

Apart from that, I would suggest using repositories that inherit from CrudRepository so you can use derived queries.除此之外,我建议使用从CrudRepository继承的存储库,以便您可以使用派生查询。

Edit2: After seeing the stack trace the problem becomes clear. Edit2:看到堆栈跟踪后,问题变得清晰。 Jackson is trying to bind the Conversation object to JSON but encounters an endless loop because of the Conversation reference in the User class. Jackson is trying to bind the Conversation object to JSON but encounters an endless loop because of the Conversation reference in the User class.

There are two ways to solve this.有两种方法可以解决这个问题。 If you still want to insert the User object referenced by a Conversation you can add the @JsonIgnore Annotation.如果您仍想插入Conversation引用的User object,您可以添加@JsonIgnore注释。

@JsonIgnore
@OneToMany(mappedBy = "User")
private List<Conversation> Conversation; 

If you don't care about the user at all, then you could also add this to the user reference in the conversation object:如果您根本不关心用户,那么您也可以将其添加到对话 object 中的用户参考中:

@JsonIgnore
@JoinColumn(name = "User_id")
private User User;

If you later encounter problems, you could also think about mapping the Conversation objects returned by your repository to specific data transfer objects which are specifically made for the JSON mapping.如果您以后遇到问题,您还可以考虑将存储库返回的Conversation对象映射到专门为 JSON 映射创建的特定数据传输对象。

Judgind by the stacktrace, and the data model the problem lies in the fact, that you have two way relationship between User and Converstaion classes.通过堆栈跟踪和数据 model 判断问题在于,您在 User 和 Converstaion 类之间有两种方式的关系。

User have a list of Conversation items, which then link back to the user.用户有一个对话项目列表,然后链接回用户。 When serializing, this creates an infinite loop :序列化时,这会创建一个无限循环

serialize user -> serialize his conversations -> serialize coversation.user -> serialize his conversations etc.序列化用户 -> 序列化他的对话 -> 序列化coveration.user -> 序列化他的对话等。

One way to avoid this is to annotate the User field in converstaion with @JsonIgnore , this way when serializing user, you'll get his conversations, but since the reference back will be ignored by Jackson, you won't get the error from the stacktrace.避免这种情况的一种方法是使用@JsonIgnore在 converstaion 中注释 User 字段,这样在序列化用户时,您将获得他的对话,但由于 Jackson 将忽略返回的引用,因此您不会从堆栈跟踪。

The exception message is pretty clear here: Infinite recursion (StackOverflowError) (through reference chain: org.club.entities.User_$$_jvstf65_b["conversation"]->org.hibernate.collection.internal.PersistentBag[0]->org.club.entities.Conversation["user"]-异常消息在这里很清楚:无限递归(StackOverflowError)(通过引用链: org.club.entities.User_$$_jvstf65_b["conversation"]->org.hibernate.collection.internal.PersistentBag[0]->org .club.entities.Conversation["user"]-

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.fasterxml.jackson.databind.ser.BeanSerializer.serialize Spring JPA - com.fasterxml.jackson.databind.ser.BeanSerializer.serialize Spring JPA java.lang.StackOverflowError: null at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields - java.lang.StackOverflowError: null at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ser/FilterProvider - java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ser/FilterProvider 服务器崩溃并出现错误:com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields - Server crashes with the error: com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields Flink启动时java.lang.ClassNotFoundException:com.fasterxml.jackson.databind.ser.FilterProvider - java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ser.FilterProvider when flink boot up 杰克逊错误 com.fasterxml.jackson.databind.ser.ContainerSerializer: 方法<init> (Lcom/fasterxml/jackson/databind/JavaType;)V 未找到 - Jackson error com.fasterxml.jackson.databind.ser.ContainerSerializer: method <init>(Lcom/fasterxml/jackson/databind/JavaType;)V not found 使用com.fasterxml.jackson.databind.ObjectMapper序列化接口字段 - serialize interface fields using com.fasterxml.jackson.databind.ObjectMapper java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonMappingException - java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonMappingException Java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException - Java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException 无法解析类型[org.glassfish.jersey.message.filtering.spi.ObjectProvider的所有bean。 <com.fasterxml.jackson.databind.ser.FilterProvider> ] - Unable to resolve any beans for Types [org.glassfish.jersey.message.filtering.spi.ObjectProvider<com.fasterxml.jackson.databind.ser.FilterProvider>]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM