Is it possible to selectively determine when the @JsonFilter annotation gets used at runtime?
I\'m getting JsonMappingException exception (see below) when I don\'t p
I had a similar issue getting the same Exception, but the accepted answer didn't really help in my case. Here's the solution that worked for me:
In my setup I was using a custom JacksonSerializer like this:
@JsonSerialize(using = MyCustomSerializer.class)
private Object someAttribute;
And that serializer was implemented like this:
public class MyCustomSerializer extends JsonSerializer
The problem with this is, that as long as you don't use any filters, it works. It also works if you serialize primitives, so for instance if you use jgen.writeString(..). If you use filters, that code is wrong, because the filters are stored somewhere inside of the SerializerProvider, not in the JsonGenerator. If in that case you use the jsongenerator directly, a new SerializerProvider, that doesn't know about the filters, is created internally. So instead of the shorter jgen.writeObject(o) you need to call provider.defaultSerializeValue(o, jgen). That will ensure that the filters don't get lost and can be applied.