我有以下POJO课程:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Folder", propOrder = {
"id",
"parent"
})
@XmlSeeAlso({
FolderTree.class
})
public class Folder
{
protected int id;
protected int parent;
getters, setters...
}
和
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "FolderTree", propOrder = {
"subFolders"
})
@XmlRootElement(name = "folderTree")
public class FolderTree extends Folder
{
@XmlElement(name = "subFolder", type = Folder.class)
protected List<Folder> subFolders;
getter...
}
和包装器:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Folders", propOrder = {
"folders"
})
@XmlRootElement(name = "folders")
public class Folders
{
@XmlElement(name = "folder", type = Folder.class)
protected List<Folder> folders;
getter...
}
我需要在XML和JSON中对它们进行序列化和反序列化。 XML工作正常,但JSON按以下结构序列化:
{
"folder": [
{
"subFolder": [
{
"subFolder": [],
"id": 2,
"parent": 1
},
{
"subFolder": [],
"id": 3,
"parent": 1
}
],
"id": 1,
"parent": null
}
]
}
反序列化器无法识别subFolder对象中的第二个字段subFolder(“subFolder”:[])。有没有办法让它只在父文件夹中包含subFolder字段?或者可以做些什么来让杰克逊将该对象反序列化呢?
我无法更改POJO类,因为它们是由jaxb创建的,并且所有内容都可以正常使用XML。
答案 0 :(得分:1)
解决方案。
为了在没有子节点的情况下摆脱空的子类数组,我在序列化功能中禁用了WRITE_EMPTY_JSON_ARRAYS。我使用Spring,bean看起来像这样:
<bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="annotationIntrospector" ref="jaxbAnnotationIntrospector" />
<property name="featuresToDisable">
<list>
<value type="com.fasterxml.jackson.databind.SerializationFeature">WRITE_EMPTY_JSON_ARRAYS</value>
</list>
</property>
</bean>
对于反序列化,因为我无法修改POJO类,所以我为Folder类添加了一个MixIn:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "folder",
defaultImpl = FolderTree.class)
public class FolderMixin {
}
最后,我扩展了ObjectMapper以设置MixInAnnotations:
public class CustomObjectMapper extends ObjectMapper
{
public CustomObjectMapper()
{
super();
customize();
}
protected void customize()
{
registerModule(new SimpleModule() {
private static final long serialVersionUID = 1L;
public void setupModule(SetupContext context) {
super.setupModule(context);
context.setMixInAnnotations(Folder.class, FolderMixin.class);
}
});
}
}
我添加了两个AnnotationIntrospectors用于反序列化:
<bean id="customObjectMapper" class="path.to.my.CustomObjectMapper">
<property name="annotationIntrospector" ref="introspectors" />
</bean>
<!-- introspectors -->
<bean id="xmlAnnotation" class="com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector">
<constructor-arg value="#{customObjectMapper.getTypeFactory()}" />
</bean>
<bean id="jsonAnnotation" class="com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector" />
<bean id="introspectors" class="com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair">
<constructor-arg ref="xmlAnnotation" />
<constructor-arg ref="jsonAnnotation" />
</bean>
<!-- end of introspectors -->
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="customObjectMapper"/>
</bean>
我使用Jackson 2.4.0。