I have finished implementing the following solution.
One end of the relationship is considered the parent. He does not need the annotation associated with Jackson.
public class Collaboration { private Set<Tag> tags; }
The other side of the relationship is implemented as follows.
public class Tag { @JsonSerialize(using = SimpleCollaborationSerializer.class) private Set<Collaboration> collaborations; }
I use a custom serializer to make sure that circular references do not occur. A serializer can be implemented as follows:
public class SimpleCollaborationSerializer extends JsonSerializer<Set<Collaboration>> { @Override public void serialize(final Set<Collaboration> collaborations, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException { final Set<SimpleCollaboration> simpleCollaborations = Sets.newHashSet(); for (final Collaboration collaboration : collaborations) { simpleCollaborations.add(new SimpleCollaboration(collaboration.getId(), collaboration.getName())); } generator.writeObject(simpleCollaborations); } static class SimpleCollaboration { private Long id; private String name; // constructors, getters/setters } }
This serializer will only show a limited set of Collaboration entity properties. Since the "tags" property is not specified, circular links will not be executed.
A good reading of this topic can be found here . It explains all the possibilities when you have a similar scenario.
tstorms
source share