org.apache.cxf.interceptor.Fault:Unmarshalling Error:遇到非法字符(NULL,unicode 0):在任何内容中无效

时间:2014-06-27 08:50:47

标签: java web-services jaxb jax-ws cxf

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

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

堆栈追踪:

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.access$100(JAXBEncoderDecoder.java:94)
        at org.apache.cxf.jaxb.JAXBEncoderDecoder$1.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

2 个答案:

答案 0 :(得分:3)

来自here

  

导致这些XmlExceptions的问题在于数据是什么   读取或加载包含根据的非法字符   XML规范。几乎总是,这些字符都在ASCII中   控制角色范围(想想像null,贝尔这样的怪人角色,   退格等)。这些不是任何业务的角色   在XML数据中;他们是应该删除的非法字符,   通常已经从文件格式中找到了进入数据的方式   转换,就像有人试图从Excel创建XML文件一样   数据,或从可以存储为的格式将其数据导出到XML   二进制的。

在解组之前清理数据的示例。 Error about invalid XML characters on Java

也许你应该使用开箱即用的东西来传输文件,比如MTOM check here

或者您可以使用CDATA syntax来包装内容,因此评估为null的部分 将以文字形式收到。

答案 1 :(得分:0)

转到项目属性并将项目编码更改为UTF-8。 对marshaller和unmarchaller也使用以下内容

    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();}

}