Java,杰克逊,将对象转换为xml,字符无效的问题

时间:2019-04-18 09:41:42

标签: java xml serialization jackson

我正在尝试使用杰克逊将对象转换为xml

XmlMapper xmlMapper = new XmlMapper();
            xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
            xmlMapper.writeValue(createFileResult.getFile(), detailsProductDTOs);

但是,由于我的对象在某处包含xml无效字符,因此出现错误。有什么方法可以告诉杰克逊忽略这种字符吗?或任何其他解决方案?

Caused by: java.io.IOException: Invalid white space character (0xb) in text to output (in xml 1.1, could output as a character entity)
    at com.ctc.wstx.api.InvalidCharHandler$FailingHandler.convertInvalidChar(InvalidCharHandler.java:56)
    at com.ctc.wstx.sw.XmlWriter.handleInvalidChar(XmlWriter.java:629)
    at com.ctc.wstx.sw.BufferingXmlWriter.writeCharacters(BufferingXmlWriter.java:583)
    at com.ctc.wstx.sw.BaseStreamWriter.writeCharacters(BaseStreamWriter.java:469)
    at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.writeString(ToXmlGenerator.java:588)

1 个答案:

答案 0 :(得分:0)

最后,我自己解决了这个问题。 如错误指示xml 1.1支持此类字符。 因此,只需添加以下配置即可解决该问题:

xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true);