In the end I've handled this in a different way. I was using Jersey and Guice so was a little hard to find out how, but I did it.
Basically I used the MixIn annotations of Jackson but using Jersey I had to create the ObjectMapper into a Provider and then bind it with Guice.
In this way when I'm using the REST service Jersey will use the ObjectMapper defined in the Provider; when storing the stuff Jackson will use the standard ObjectMapper.
Now some code.
create the PrivateUser class:
public abstract class PrivateUser {
@JsonIgnore abstract String getPassword();
}
create the provider:
@Provider
public class JacksonMixInProvider implements ContextResolver<ObjectMapper> {
@Override
public ObjectMapper getContext(Class<?> aClass) {
final ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().addMixInAnnotations(User.class, PrivateUser.class);
return mapper;
}
}
and bind it:
bind(JacksonMixInProvider.class).asEagerSingleton();
That's it! :D
Hope this will help someone else to waste less time!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…