Axis2 error: Invalid white space character (0x4) in text to output

天涯浪子 提交于 2019-12-11 06:09:46

问题


I have created a Java client to interact with a SOAP webservice using Axis2 (1.7.6) as code generator. The problem is with some inputs the client is throwing an exception with the message:

org.apache.axis2.AxisFault: Invalid white space character (0x4) in text to output (in xml 1.1, could output as a character entity)

It seems the serialiser is hitting some chars considered invalid to XML spec. I have seen that problem around but no definitive answer or the fix. I'm not using Spring or any other dependency injection framework, it's a standalone application, so I need to configure the inners of Axis2 by hand.

Any ideas on how to fix/configure the client properly?


回答1:


After some research I found this behaviour is due to one default setting of the lib Woodstox (Axis2 dependency), that uses the class com.ctc.wstx.api.InvalidCharHandler.FailingHandler as default implementation of the interface com.ctc.wstx.api.InvalidCharHandler, used inside com.ctc.wstx.sw.XmlWriter and invoked in the serialisation process. This means: when the component hits characters considered invalid to XML, it’ll throw an error.

Woodstox provides another implementation of the interface com.ctc.wstx.api.InvalidCharHandler, the one called com.ctc.wstx.api.InvalidCharHandler.ReplacingHandler that instead of throwing errors will replace those chars for something else. But how to do that?

The class com.ctc.wstx.stax.WstxOutputFactory inside Woodstox contains several configurations, one of them being the invalid char handler. Though, it's not configurable by some magic system wide property, instead, by the method com.ctc.wstx.stax.WstxOutputFactory#setProperty, that takes as arguments one string and one object.

So first, you'll have to extend that factory and set the property com.ctc.wstx.outputInvalidCharHandler with an instance of com.ctc.wstx.api.InvalidCharHandler.ReplacingHandler that takes as argument the char you want to replace the invalid ones with. Like this:

package my.package;

import com.ctc.wstx.stax.WstxOutputFactory;
public class MyWstxOutputFatory extends WstxOutputFactory {

    public MyWstxOutputFatory() {
        setProperty(
                com.ctc.wstx.api.WstxOutputProperties.P_OUTPUT_INVALID_CHAR_HANDLER,
                new com.ctc.wstx.api.InvalidCharHandler.ReplacingHandler(' '));
    }
}

The second, trickiest and undocumented step is how to register your implementation as the factory Woodstox'll use. You'll have to create a file named META-INF/services/javax.xml.stream.XMLOutputFactory simply containing the name of your factory, in this case, the string:

my.package.MyWstxOutputFatory

Place this file in such a way it's included in your project's resulting jar. In my case I placed like: src/main/resources/META-INF/services/javax.xml.stream.XMLOutputFactory.

And you're done!



来源:https://stackoverflow.com/questions/47421275/axis2-error-invalid-white-space-character-0x4-in-text-to-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!