1

I've got an entity with the following signature:

public class Record extends BaseEntity<Long, Record> {...}

Now I want to send it via REST using JSON. Problem is, the self reference. Because of that I get the following error.

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Direct self-reference leading to cycle (through reference chain: Record["this"])

What can I do, e.g. which annotation, to convert this object to JSON?

LStrike
  • 1,598
  • 4
  • 26
  • 58
  • Could you share your BaseEntity and Record class? – Yug Singh Sep 20 '18 at 09:13
  • Sorry, not possible, to many internal features of my company and it is way to big (> 300 lines of code) – LStrike Sep 20 '18 at 09:15
  • 1
    What have you tried so far? Maybe this can help you: https://stackoverflow.com/questions/20218568/direct-self-reference-leading-to-cycle-exception – svdragster Sep 20 '18 at 09:23
  • 1
    @LStrike: Check this out https://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue – Abhishek Sep 20 '18 at 10:27

1 Answers1

0

You can use @JsonIgnore annotation in your BaseEntity class above the generic instance variable which is referring to record class. It ignore one of the sides of the relationship, thus breaking the chain.

Alternatively, you can use @JsonManagedReference, @JsonBackReference. You can read more about it here

Yug Singh
  • 3,112
  • 5
  • 27
  • 52