杰克逊在序列化期间忽略注释
Jackson ignores Annotations during serialization
我有以下结构要序列化
@JsonTypeName("DS")
public class Elemencik implements IC {
private String item;
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public Elemencik() {
}
}
@JsonTypeInfo(use= JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.PROPERTY, property="type")
@JsonSubTypes({
@JsonSubTypes.Type(Elemencik.class)
})
public interface IC {
}
我使用 ObjectMapper 序列化 Elemencik 实例 class。
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.USE_ANNOTATIONS, true);
IC mm = new Elemencik();
String res = mapper.writeValueAsString(mm);
序列化后,我看到映射器忽略了有关注释 @JsonTypeName("DS") 中包含的类型的信息。所以 final json 没有字段类型。
这是我的 pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.11.1</version>
</dependency>
可能是什么问题?
如果您的导入是
,我认为您的 import
有问题
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
然后它不能序列化类型字段,你有这个字符串:
{"item":null}
但如果您的导入是:
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
代码是:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.USE_ANNOTATIONS, true);
IC mm = new Elemencik();
String res = mapper.writeValueAsString(mm);
它可以序列化类型字段并且你有以下字符串:
{"type":"DS","item":null}
我有以下结构要序列化
@JsonTypeName("DS")
public class Elemencik implements IC {
private String item;
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public Elemencik() {
}
}
@JsonTypeInfo(use= JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.PROPERTY, property="type")
@JsonSubTypes({
@JsonSubTypes.Type(Elemencik.class)
})
public interface IC {
}
我使用 ObjectMapper 序列化 Elemencik 实例 class。
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.USE_ANNOTATIONS, true);
IC mm = new Elemencik();
String res = mapper.writeValueAsString(mm);
序列化后,我看到映射器忽略了有关注释 @JsonTypeName("DS") 中包含的类型的信息。所以 final json 没有字段类型。 这是我的 pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.11.1</version>
</dependency>
可能是什么问题?
如果您的导入是
,我认为您的import
有问题
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
然后它不能序列化类型字段,你有这个字符串:
{"item":null}
但如果您的导入是:
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
代码是:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.USE_ANNOTATIONS, true);
IC mm = new Elemencik();
String res = mapper.writeValueAsString(mm);
它可以序列化类型字段并且你有以下字符串:
{"type":"DS","item":null}