尝试将统计表源转换为StreamResult时出现错误(在使用Adoptopenjdk 11-HotSpot在WildFly 24上运行时)
但是从单元测试中执行的相同代码没有错误地通过,并且正确地完成了工作(使用Adoptopenjdk 11-HotSpot)
我收到的错误如下:
14:50:15,455 ERROR [com.x.x.x.x.x.DeliveryParser] (default task-1) javax.xml.transform.TransformerException: Source object passed to ''{0}'' has no contents.
ERROR: 'Source object passed to ''{0}'' has no contents.'
这里是源代码:
private static String readElementString(final XMLStreamReader streamReader) {
try {
StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.toString());
StAXSource stAXSource = new StAXSource(streamReader);
StreamResult streamResult = new StreamResult(stringWriter);
transformer.transform(stAXSource, streamResult); <-- error is thrown here
return stringWriter.toString();
...
找不到任何我可以在网上使用的东西。
有没有人给我一个提示?
Thx-Fabien
我找到的解决方案:
在进行单元测试时,";TransformerFactory";给我一个来自JDK的";com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl";实例。
在Wildlfy上,工厂给了我一个";org.apache.xalan.xsltc.trax.TransformerImpl";.实例当我尝试转换SaxSource时,最后一个错误会导致错误。我不知道为什么。
因此,我现在使用下面的代码从JDK:
实例化TransformerFactoryTransformerFactory.newDefaultInstance();
代替
TransformerFactory.newInstance();