Siddharth Trikha Siddharth Trikha - 1 year ago 402
Java Question

Jackson custom serializer by field name?

I have a POJO to be serialized which has field of type

and I have a custom serializer written for it.


@XmlType(name = "user", propOrder = {
public class User {

protected User.Event userEvent;
protected Boolean isValid;
protected Boolean isPrivleged;

// getter/ setters

// Inner static class
public static class Event {

protected Object representation;
protected User.Event.Monitor userMonitor;

// getter setters and Monitor static class


Now, I cannot edit my POJO for some reason, so I want all configurations of Jackson in code via
. I am not able to register my custom serializer for field
Object representation
as it handles type
which is a super class for all.

public class CustomSerializer extends JsonSerializer<Object>{

public void serialize(Object obj, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {

// ...........

public Class<Object> handledType() {
return Object.class;


This gives exception:

java.lang.IllegalArgumentException: JsonSerializer of type CustomSerializer does not define valid handledType() -- must either register with method that takes type argument or make serializer extend 'com.fasterxml.jackson.databind.ser.std.StdSerializer'
at com.fasterxml.jackson.databind.module.SimpleSerializers.addSerializer(
at com.fasterxml.jackson.databind.module.SimpleModule.addSerializer(

So I guess since every field has superclass
, thus it's saying invalid handleType().

Is there a way to register a Serializer programtically via the fieled name or something. Eg When field name is
register my serializer for it ??

How to handle the above case ??

Answer Source

Have you ever considered Jackson mix-in annotations?

Jackson mix-in annotations

It's a great alternative when modifying the classes is not an option. You can think of it as kind of aspect-oriented way of adding more annotations during runtime, to augment statically defined ones.

Define a mix-in annotation interface (class would do as well):

public interface EventMixIn {

    @JsonSerialize(using = CustomSerializer.class)
    Object getRepresentation();

Then configure ObjectMapper to use the defined interface as a mix-in for your POJO:

ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT)
                                        .addMixIn(User.Event.class, EventMixIn.class); 

Usage considerations

Here are some usage considerations:

  • All annotation sets that Jackson recognizes can be mixed in.
  • All kinds of annotations (member method, static method, field, constructor annotations) can be mixed in.
  • Only method (and field) name and signature are used for matching annotations: access definitions (private, protected, ...) and method implementations are ignored.

For more details, you can have a look at this page.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download