1

I have the following two enums

public enum Action {
    ACTION1,
    ACTION2,
    ACTION3;
}
public enum EntityType {
    ENTITYTYPE1,
    ENTITYTYPE2;
}

and the following class

public class EntityIdentityDto implements MetaData {
    private String id;
    private EntityType entityType;
    private Action action;
    private Map<String, Object> properties = new HashMap();

    public String getId() {
        return this.id;
    }

    public EntityType getEntityType() {
        return this.entityType;
    }

    public Action getAction() {
        return this.action;
    }

    public Map<String, Object> getProperties() {
        return this.properties;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setEntityType(EntityType entityType) {
          this.entityType = entityType;
    }

    public void setAction(Action action) {
        this.action = action;
    }

    public void setProperties(Map<String, Object> properties) {
        this.properties = properties;
    }

    public EntityIdentityDto() {
    }

}

When using Jackson 2.9.8 to serialize into Json as per below

public class TestMe {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        EntityIdentityDto entityIdentityDto = new EntityIdentityDto();
        entityIdentityDto.setEntityType(EntityType.ENTITYTYPE1);
        entityIdentityDto.setAction(Action.ACTION1);
        entityIdentityDto.setId("OOO");
        String out = objectMapper.writeValueAsString(entityIdentityDto);
        System.out.println(out);
    }
}

The output is

{"id":"OOO","action":"ACTION1","properties":{}}

I am expecting to so the entityType field serialized as well but this is missing. This is what I expect to see

{"id":"OOO","entityType": "ENTITYTYPE1", "action":"ACTION1","properties":{}}

If instead of Jackson I use Gson as per below

public class TestMe {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        EntityIdentityDto entityIdentityDto = new EntityIdentityDto();
        entityIdentityDto.setEntityType(EntityType.SYSTEM);
        entityIdentityDto.setAction(Action.SYNC);
        entityIdentityDto.setId("OOO");
        System.out.println(new Gson().toJson(entityIdentityDto));
    }
}

The output is as expected

{"id":"OOO","entityType":"ENTITYTYPE1","action":"ACTION1","properties":{}}

Why is the entityType field missing in the Json generated using Jackson ? It is interesting that action gets serialized but entityType does not even though they are structurally identical and used identically in the EntityIdentityDto

Khetho Mtembo
  • 388
  • 6
  • 20
  • For me, Jackson also produced the same output. Which version of Jackson are you using? I tried it on version `2.10.0.pr3`. Probably you are missing getters? – Mushif Ali Nawaz Oct 05 '19 at 18:45
  • I would guess this is an environment/configuration/forgot-to-recompile-all-classes type error – flakes Oct 05 '19 at 18:47
  • I would guess this is also environment/configuration/forgot-to-recompile-all-classes type issue but can confirm I have cleaned the environment, rebuilt using both the IDE and gradle with the same result. – Khetho Mtembo Oct 05 '19 at 19:04
  • @KhethoMtembo, how `ObjectMapper` configuration looks like? Do you use any modules, custom serialisers, filters, etc.? – Michał Ziober Oct 05 '19 at 19:19

2 Answers2

0

Probably you do not have getter for entityType property. Add getter or use setVisibility method:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

See also:

  1. how to specify jackson to only use fields - preferably globally
  2. Jackson – Decide What Fields Get Serialized/Deserialized
  3. Exclude Fields from Serialization in Gson
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
0

The issue was with my EntityIdentityDto class which implements the following interface

public interface MetaData {
    String getId();
    Map<String, Object> getProperties();
    void setProperties(Map<String, Object> properties);

    @JsonIgnore
    EntityType getEntityType();
}

The JsonIgnore at the interface level is the reason why it was not being serialized. After dropping the JsonIgnore all works as expected now.

Khetho Mtembo
  • 388
  • 6
  • 20