杰克逊未序列化属性

问题描述 投票:1回答:2

我有以下两个枚举

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

和以下课程

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() {
    }

}

根据以下使用Jackson 2.9.8序列化为Json时

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);
    }
}

输出为

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

我期望也将EntityType字段序列化,但这是丢失的。这是我希望看到的

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

如果不是代替杰克逊,我按如下方式使用Gson

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));
    }
}

输出符合预期

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

为什么使用Jackson生成的Json中的entityType字段丢失?有趣的是,操作被序列化了,但entityType却不一样,即使它们在结构上相同并且在EntityIdentityDto]中使用相同

我有以下两个枚举public枚举Action {ACTION1,ACTION2,ACTION3; }公共枚举EntityType {ENTITYTYPE1,ENTITYTYPE2; }和以下类public class ...

java json jackson2
2个回答
0
投票

[您可能没有entityType属性的获取者。添加吸气剂或使用setVisibility方法:


0
投票

问题出在我的EntityIdentityDto类中,该类实现了以下接口

© www.soinside.com 2019 - 2024. All rights reserved.