i have a javaEE application with two entities which gets persistet in a database. both entities have a bi-directional association.
First Entity:
@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope=Child.class)
public class Child implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(fetch= FetchType.LAZY)
private Father father;
}
Second Entity
@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope=Father.class)
public class Father implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(fetch= FetchType.LAZY)
private Child child;
}
Both are exposed via ressources like:
@Path("father")
@Stateless
public class FatherResource{
@Context
private UriInfo uri;
@Inject FatherDao fatherDao;
public FatherResource() {
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Father> getFathers() {
//TODO return proper representation object
return fatherDoa.getFathers();
}
}
There is also a DAO which just gets the Father-Object from the database.
The problem is now, that i get a circular json structure. so something like:
{ "id":"1", "child": {"id":"1", "father": {"id":"1", "child":{"id":"1", [...]
I just want to see the child once.
I tried to use:
@JsonIgnoreProperties({"child"}) //above the class
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id") //above the class
@JsonIgnore //above the property
what i want to get is:
{ "id":"1", "child": {"id":"1"}}
I'm using
JDK 7
JaxRS
Jackson
Hibernate/Eclipselink
Glassfish 4.0
another test which doesn't work:
@Entity
@JsonSerialize
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id", scope=Object.class)
public class Father implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(fetch= FetchType.LAZY)
private Child child;
}
ApplicationConfig.java:
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
// following code can be used to customize Jersey 2.0 JSON provider:
try {
Class jsonProvider = Class.forName("org.glassfish.jersey.jackson.JacksonFeature");
} //...
//..
beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
</beans>