杰克逊没有连载 属性
Jackson not serializing property
我有以下两个枚举
public enum Action {
ACTION1,
ACTION2,
ACTION3;
}
public enum EntityType {
ENTITYTYPE1,
ENTITYTYPE2;
}
及以下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() {
}
}
当使用 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 而不是 Jackson,如下所示
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 字段?
有趣的是 action 被序列化但 entityType 没有序列化,即使它们在结构上相同并且在 EntityIdentityDto
中使用相同
可能您没有 getter entityType
属性。添加getter或使用setVisibility方法:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
另请参阅:
- how to specify jackson to only use fields - preferably globally
- Jackson – Decide What Fields Get Serialized/Deserialized
- Exclude Fields from Serialization in Gson
问题出在我的 EntityIdentityDto class 上,它实现了以下接口
public interface MetaData {
String getId();
Map<String, Object> getProperties();
void setProperties(Map<String, Object> properties);
@JsonIgnore
EntityType getEntityType();
}
接口层的JsonIgnore是没有被序列化的原因。删除 JsonIgnore 后,现在一切正常。
我有以下两个枚举
public enum Action {
ACTION1,
ACTION2,
ACTION3;
}
public enum EntityType {
ENTITYTYPE1,
ENTITYTYPE2;
}
及以下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() {
}
}
当使用 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 而不是 Jackson,如下所示
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 字段? 有趣的是 action 被序列化但 entityType 没有序列化,即使它们在结构上相同并且在 EntityIdentityDto
中使用相同可能您没有 getter entityType
属性。添加getter或使用setVisibility方法:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
另请参阅:
- how to specify jackson to only use fields - preferably globally
- Jackson – Decide What Fields Get Serialized/Deserialized
- Exclude Fields from Serialization in Gson
问题出在我的 EntityIdentityDto class 上,它实现了以下接口
public interface MetaData {
String getId();
Map<String, Object> getProperties();
void setProperties(Map<String, Object> properties);
@JsonIgnore
EntityType getEntityType();
}
接口层的JsonIgnore是没有被序列化的原因。删除 JsonIgnore 后,现在一切正常。