gavinklfong / stream-api-exercises

Java Stream API Exercises
79 stars 160 forks source link

Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) #9

Open AbulSyed opened 1 year ago

AbulSyed commented 1 year ago

Hi I have cloned the project directly and created an REST endpoint each to fetch the products and orders using the findAll() method. When I try to invoke the endpoint using Postman, to see the data output, I am getting the below error:

Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.hibernate.collection.internal.PersistentSet[0]->space.gavinklfong.demo.streamapi.models.Product["orders"]->org.hibernate.collection.internal.PersistentSet[0]->space.gavinklfong.demo.streamapi.models.Order["products"]....

Looks like the infinite json issue is caused due the mapping between the product and order entity.

gavinklfong commented 1 year ago

Got your issue. This exercise was designed to run only the unit tests with partial JPA spring context in order to demonstrate the Java stream function calls. It might not work properly when loading the full Spring context.

The infinite recursion issue is probably due to conflict between Lombok @Data and @ManyTo@Many annotation. Refer to this stackoverflow issue. I will apply a fix to it and let you know. Thanks.