I have a 2 Java classes that look like this:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
Class A {
private B b;
...
some more fields
...
public B getB() {...}
public void setB(B b){...}
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
Class B {
private A a;
...
some more fields
...
public A getA() {...}
public void setA(A a){...}
}
Trying to deserialize object A, I get this error:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Invalid Object Id definition for
A: cannot find property with name 'id'
I thought that using the annotation on the class level adds the id property to the Json. Is it correct? How can it be solved?
Using generator = ObjectIdGenerators.PropertyGenerator.class indicates that you want to used either a field or a getter method as "identifier".
Using property = "id" show that you want the field id used as "identifier", which is redondant, according to javadoc, because "id" is the default value when not written in the annation property field.
So you can just write @JsonIdentityInfo(ObjectIdGenerators.PropertyGenerator.class)
Now, about your error, you HAVE to provide the id field in your class. The annotation does not create code, it is only metadata on code(if you think about lombok, forget it, it is stupid library).
Annotation is processed on runtime via introspection by jackson so that it know how to work with your class (it uses a PropertyGenerator, and default field name is 'id')