Custom Jackson Serializer for a specific type in a particular class

You should use MixIn feature. In your example you have to create new interface:

interface ClassAMixIn {

    @JsonSerialize(using = ByteArrayJacksonSerializer.class)
    byte[] getToken();
}

which specifies custom serializer for given property. Now we have to configure ObjectMapper

ObjectMapper mapper = new ObjectMapper();
mapper.addMixInAnnotations(ClassA.class, ClassAMixIn.class);

Your custom serializer will be used only for serializing byte array property in ClassA.

Leave a Comment