I have a setup with Resteasy-3.0.6, Jackson-2.2.3 and CDI (running on Wildfly-8.0.0.CR1). In my case, every entity has a mix-in class that extends it and specifies the attributes that are to be serialized. There are two "views", let's call them Basic and Extended. Since what kind of view of an object I need depends on the "root" object of the serialization, these views need to be per object. So I re-use the mix-in classes for that. Example:
public class Job {
@Id private Long id;
@OneToMany(mappedBy="job") private Set<Bonus> bonuses;
}
public class Bonus {
@Id private Long id;
@ManyToOne(optional=false) private Job job;
private BigDecimal value;
}
public abstract class JsonJob extends Job {
abstract Long getId();
@JsonView({ JsonJob.class })
abstract Set<Bonus> getBonuses();
}
public abstract class JsonBonus extends Bonus {
abstract BigDecimal getValue();
}
Now I'm looking for a way to hook into jackson's serialization process to specify JsonJob
as a view iff the root entity is Job
. I'm currently using JacksonJsonProvider#writeTo
:
@Override
public void writeTo(Object value, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException {
Class view = getViewForType(type); // if no mix-in class is set, return Object
ObjectMapper mapper = locateMapper(type, mediaType);
// require all properties to be explicitly specified for serialization
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
mapper.registerModule(new SerializationMixinConf()); // registers all mix-ins
super.writeTo(value, type, genericType, annotations, mediaType,
httpHeaders, entityStream);
}
The point is: this works, iff the calling method is annotated @JsonView
. But since the main user of this is a generic super-class, I can't add the annotation to the method directly. Can someone suggest a way to either set the view in the existing mapper (setSerializationConfig()
seems to be gone in jackson-2) or dynamically add the @JsonView
to the annotations
array?