Java org.apache.cxf.interceptor.Fault:解组错误:遇到非法字符(NULL,unicode 0):在任何内容中都无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24447695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:15:30  来源:igfitidea点击:

org.apache.cxf.interceptor.Fault: Unmarshalling Error: Illegal character (NULL, unicode 0) encountered: not valid in any content

javaweb-servicesjaxbjax-wscxf

提问by Shriram

I am using a cxf webservice which uses local transport and accessing the webservice from java application. Webservice is reading a file and sending through webservice call. I am using the byte size as 512. Suppose the file size is 1200. First two attempt of retrieving the file is success and for the last chunk i am getting org.apache.cxf.interceptor.Fault: Unmarshalling Error: Illegal character (NULL, unicode 0) encountered: not valid in any content.

我正在使用 cxf webservice,它使用本地传输并从 java 应用程序访问 webservice。Webservice 正在读取文件并通过 webservice 调用发送。我使用的字节大小为 512。假设文件大小为 1200。检索文件的前两次尝试成功,最后一个块我得到 org.apache.cxf.interceptor.Fault: Unmarshalling Error: Illegal character (NULL , unicode 0) 遇到:在任何内容中都无效。

Here chunk represents 512 bytes. I am converting bytes to string and returning from web service.In the last chunk only 16 byte value is filled and remaining are filled with zeros. Any help will be appreciated. I am using cxf webservice 2.7.5, jdk1.7 ,Redhat Linux.

这里的块代表 512 个字节。我正在将字节转换为字符串并从 Web 服务返回。在最后一个块中,只填充了 16 个字节的值,其余的用零填充。任何帮助将不胜感激。我正在使用 cxf webservice 2.7.5、jdk1.7、Redhat Linux。

Stack trace:

堆栈跟踪:

org.apache.cxf.interceptor.Fault: Unmarshalling Error: Illegal character (NULL, unicode 0) encountered: not valid in any content
 at [row,col {unknown-source}]: [2,1]
        at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:808)
        at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:629)
        at org.apache.cxf.jaxb.io.DataReaderImpl.read(DataReaderImpl.java:157)
        at org.apache.cxf.interceptor.DocLiteralInInterceptor.handleMessage(DocLiteralInInterceptor.java:103)
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
        at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:800)
        at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1592)
        at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1490)
        at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1309)
        at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
        at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:622)
        at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
        at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:530)
        at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:463)
        at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:366)
        at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:319)
        at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
        at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:133)
        at $Proxy62.getJobLog(Unknown Source)
        at com.java.process.AClass.getMessage(AClass.java:468)
        at com.java.process.AClass.getXXXX(AClass.java:156)
        at com.java.process.AClass.main(CLIClientStartup.java:409)
Caused by: javax.xml.bind.UnmarshalException
 - with linked exception:
[com.ctc.wstx.exc.WstxUnexpectedCharException: Illegal character (NULL, unicode 0) encountered: not valid in any content
 at [row,col {unknown-source}]: [2,1]]
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:436)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:372)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:349)
        at org.apache.cxf.jaxb.JAXBEncoderDecoder.doUnmarshal(JAXBEncoderDecoder.java:769)
        at org.apache.cxf.jaxb.JAXBEncoderDecoder.access0(JAXBEncoderDecoder.java:94)
        at org.apache.cxf.jaxb.JAXBEncoderDecoder.run(JAXBEncoderDecoder.java:797)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:795)
        ... 23 more
Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Illegal character (NULL, unicode 0) encountered: not valid in any content
 at [row,col {unknown-source}]: [2,1]
        at com.ctc.wstx.sr.StreamScanner.constructNullCharException(StreamScanner.java:630)
        at com.ctc.wstx.sr.StreamScanner.throwInvalidSpace(StreamScanner.java:660)
        at com.ctc.wstx.sr.BasicStreamReader.readTextPrimary(BasicStreamReader.java:4576)
        at com.ctc.wstx.sr.BasicStreamReader.nextFromTree(BasicStreamReader.java:2879)
        at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1072)
        at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:196)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:370)
        ... 29 more

回答by Alkis Kalogeris

From here

这里

The problem that causes these XmlExceptions is that the data being read or loaded contains characters that are illegal according to the XML specifications. Almost always, these characters are in the ASCII control character range (think whacky characters like null, bell, backspace, etc). These aren't characters that have any business being in XML data; they're illegal characters that should be removed, usually having found their way into the data from file format conversions, like when someone tries to create an XML file from Excel data, or export their data to XML from a format that may be stored as binary.

导致这些 XmlExceptions 的问题是正在读取或加载的数据包含根据 XML 规范非法的字符。几乎总是,这些字符在 ASCII 控制字符范围内(想想像 null、bell、退格等古怪的字符)。这些不是在 XML 数据中有任何业务的字符;它们是应该删除的非法字符,通常是通过文件格式转换进入数据的,例如当有人试图从 Excel 数据创建 XML 文件时,或者将他们的数据从可能存储为的格式导出到 XML 时二进制。

And an example for sanitizing the data before unmarshal. Error about invalid XML characters on Java

以及在解组之前清理数据的示例。 关于 Java 上无效 XML 字符的错误

Maybe you should use something out of the box for transferring files, like MTOM check here

也许你应该使用开箱即用的东西来传输文件,比如在这里检查MTOM

Or you could use CDATA syntaxto wrap the content, so the part that evaluates to null will be received as literal.

或者您可以使用CDATA 语法来包装内容,因此评估为 null 的部分将作为文字接收。

回答by Bassel Kh

Go to project properties and change the project encoding to UTF-8. Also use the following for marshaller and unmarchaller

转到项目属性并将项目编码更改为 UTF-8。还可以将以下内容用于 marshaller 和 unmarhaller

    private static void exportToXML(JAXBContext ctx , Object obj , OutputStream stream) throws Exception{
    try {
        Marshaller m = ctx.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

        m.marshal(obj, stream);
    } catch (JAXBException e) {
        e.printStackTrace();
    }       
}

private static Object importXML(JAXBContext ctx , String xmlStr) throws Exception {
    try {           
        Unmarshaller m = ctx.createUnmarshaller();
        ByteArrayInputStream is = new ByteArrayInputStream(xmlStr.getBytes("UTF-8"));

        return m.unmarshal(is);

    } catch (Throwable e) {e.printStackTrace();}

}