Jackson object mapping - map incoming JSON field to protected property in base class
Posted
by
Pete
on Stack Overflow
See other posts from Stack Overflow
or by Pete
Published on 2012-09-28T09:36:10Z
Indexed on
2012/09/28
9:37 UTC
Read the original article
Hit count: 505
We use Jersey/Jackson for our REST application. Incoming JSON strings get mapped to the @Entity objects in the backend by Jackson to be persisted.
The problem arises from the base class that we use for all entities. It has a protected id property, which we want to exchange via REST as well so that when we send an object that has dependencies, hibernate will automatically fetch these dependencies by their ids.
Howevery, Jackson does not access the setter, even if we override it in the subclass to be public. We also tried using @JsonSetter but to no avail. Probably Jackson just looks at the base class and sees ID is not accessible so it skips setting it...
@MappedSuperclass
public abstract class AbstractPersistable<PK extends Serializable> implements Persistable<PK> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private PK id;
public PK getId() {
return id;
}
protected void setId(final PK id) {
this.id = id;
}
Subclasses:
public class A extends AbstractPersistable<Long> {
private String name;
}
public class B extends AbstractPersistable<Long> {
private A a;
private int value;
// getter, setter
// make base class setter accessible
@Override
@JsonSetter("id")
public void setId(Long id) {
super.setId(id);
}
}
Now if there are some As in our database and we want to create a new B via the REST resource:
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response create(B b) {
if (b.getA().getId() == null)
cry();
}
with a JSON String like this {"a":{"id":"1","name":"foo"},"value":"123"}
.
The incoming B will have the A reference but without an ID.
Is there any way to tell Jackson to either ignore the base class setter or tell it to use the subclass setter instead?
I've just found out about @JsonTypeInfo
but I'm not sure this is what I need or how to use it.
Thanks for any help!
© Stack Overflow or respective owner