Hello -
I have a quick question relating to serializing a complex JPA graph to JSON
in a Jersey request. Let's say I have:
@XmlRootElement
@Entity
public Contact {
private int id;
private String firstName;
private String lastName;
@ManyToOne
private Mailbox;
}
@XmlRootElement
@Entity
public Mailbox {
private int id;
private String name;
@OneToMany(mappedBy="mailbox")
private List<Contact> contacts;
}
In my Jersey resource, I have:
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Mailbox> listMailboxes() {
MailboxService service =
Services.getInstance().getMailboxService();
return service.getMailboxes(1);
}
Basically, I'm trying to avoid serializing the Mailbox object listed in the
Contact object. Instead, I'd like to serialize it's id (calling it
"mailboxId"). Something like:
[
{"id":1,"name":"Mailbox1", "contacts":
[
{"id":1,"firstName":"JoeA","lastName":"SchmoeA","mailboxId":"1"},
{"id":2,"firstName":"JoeB","lastName":"SchmoeB","mailboxId":"1"},
{"id":3,"firstName":"JoeC","lastName":"SchmoeC","mailboxId":"1"}
]
},
{"id":2,"name":"Mailbox2", "contacts":
[
{"id":4,"firstName":"JoeD","lastName":"SchmoeD","mailboxId":"1"},
{"id":5,"firstName":"JoeE","lastName":"SchmoeE","mailboxId":"1"},
{"id":6,"firstName":"JoeF","lastName":"SchmoeF","mailboxId":"1"}
]
}
]
Any guidance would be much appreciated!