我使用Jackson来支持Jackson和JAXB注释,并将对象序列化为XML。
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.registerModule(new JacksonXmlModule());
xmlMapper.registerModule(new JaxbAnnotationModule());
xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
或者,我尝试配置
AnnotationIntrospector
具有相同的结果。
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.setAnnotationIntrospector(
new AnnotationIntrospectorPair(new XmlJaxbAnnotationIntrospector(), new JacksonAnnotationIntrospector()));
xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
但是,使用JAXB XmlEmelemt的required属性注释的POJO字段将被忽略,因为该标志被JsonInclude覆盖。包括NON\u NULL序列化策略(忽略NULL元素,不添加空标记)。
@XmlElement(name = "some-value", required = true)
protected String someValue;
有没有一种方法可以保持这种策略,但要尊重JAXB的required标志,并在每次没有值时都编写一个空元素?