Spring MVC - JSON infinite recursion

I've run into this before. But after moving @JsonIgnore from private field to getter of the field, infinite recursion is gone. So my wild guess is that @JsonIgnore might no work on private field. However, javadoc or tutorial of Jackson Java JSON-processor do not mention about this, so I cannot be 100% sure. Just for your information.


The following link says you should annotate the method used by JSON tool to traverse the object graph, to instruct the it to ignore the traversal.

http://jackson.codehaus.org/1.0.1/javadoc/org/codehaus/jackson/annotate/JsonIgnore.html

In my case I have two objects related like this Product <-> ProductImage. So JSON parser went into an infinite loop with out @JsonIgnore annotation on the following to get methods

@JsonIgnore
public Product getImageOfProduct() {
    return imageOfProduct;
}

in ProductImage and

@JsonIgnore
public Set<ProductImage> getProductImages() {
    return productImages;
}

in Product.

With the annotation, things are working fine.


I know this question isn't specifically about Spring Data REST, but I ran into this exception in the context of Spring Data REST, and wanted to share what the problem was. I had a bidirectional relationship involving an entity with no repository. Creating the repository made the loop disappear.