我需要jackson-dataformat-xml的帮助。我需要将使用XmlMapper的List<String>序列化为编码" "→的报价。
但在序列化之后,XmlMapper会对所有其他特殊符号(<、>、&等)进行编码,但会忽略引号('和")……如果我在序列化之前手动对字符串进行编码,内容会变得一团糟,因为"内部有'&',而不是序列化为&quot;。
也许有人知道如何让它工作?另外,有没有办法使用@JacksonRawValue或类似的东西来禁用List<String>字段上的自动特殊符号编码?这个注释在简单的(非数组)字段上工作得很好,但在List<String>上就不能正常工作了。
谢谢。
发布于 2019-06-28 08:55:00
下面是如何解决这个问题的。我使用的是woodbox Stax2扩展。这很有帮助。https://github.com/FasterXML/jackson-dataformat-xml/issues/75
XmlMapper xmlMapper = new XmlMapper(module);
xmlMapper.getFactory().getXMLOutputFactory().setProperty(XMLOutputFactory2.P_TEXT_ESCAPER,
new CustomXmlEscapingWriterFactory());这是工厂。
public class CustomXmlEscapingWriterFactory implements EscapingWriterFactory {
public Writer createEscapingWriterFor(final Writer out, String enc) {
return new Writer(){
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
String val = "";
for (int i = off; i < len; i++) {
val += cbuf[i];
}
String escapedStr = StringEscapeUtils.escapeXml(val);
out.write(escapedStr);
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public void close() throws IOException {
out.close();
}
};
}
public Writer createEscapingWriterFor(OutputStream out, String enc) {
throw new IllegalArgumentException("not supported");
}
}https://stackoverflow.com/questions/56799368
复制相似问题